schema-lens
Introspects MariaDB databases through the mysql2 driver, emitting compact schema notation with columns, primary/unique keys, indexes, and foreign keys, and exposing the same overview, table detail, join-path and schema-diff MCP tools as MySQL.
Introspects MySQL databases via information_schema to generate compact schema summaries (columns, keys, indexes, constraints, foreign key graph) plus snapshot-based drift detection, all under a read-only session.
Introspects PostgreSQL databases (including multi-schema targets and views) via pg_catalog, producing an ultra-compact token-optimized schema notation. Provides MCP tools to list tables with row/column estimates and foreign key edges, fetch full column, key, index and constraint details, compute shortest join paths across foreign keys, and diff schema drift against a saved snapshot.
Introspects SQLite database files via sqlite_schema in a read-only connection, producing the same compact, token-efficient schema representation with tables, columns, keys, indexes, and foreign key relationships for LLM consumption.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
Schema Lens
Token-efficient database schema for AI coding assistants (MCP server + CLI)
Schema Lens introspects PostgreSQL, MySQL, and SQLite databases and converts catalog structures into an ultra-compact, token-optimized notation designed specifically for LLM context windows. It cuts token consumption by 81% to 95% compared to traditional JSON snapshots or information_schema dumps, allowing AI coding assistants (Claude Code, Antigravity, Cursor, Windsurf) to understand entire database architectures without blowing prompt budgets.
Token Comparison
Measured on test/fixtures/shop.sql (9 tables, views, enums, composite foreign keys, indexes, check constraints) using gpt-tokenizer (OpenAI & Claude BPE):
Format | Characters | Tokens | Token Savings | Reduction Ratio | Context Budget Impact |
Pretty JSON Snapshot | 10,609 | 2,644 | 0% | 1.00x | Burns context window rapidly |
Schema Lens Compact ( | 1,711 | 493 | 81.4% | 5.36x | Fits full schema in single response |
Schema Lens Overview ( | 317 | 110 | 95.8% | 24.04x | Ultra-cheap map for initial exploration |
Related MCP server: mcp-db-analyzer
30-Second Quick Start
1. Initialize configuration
npx schema-lens-mcp initThis writes schema-lens.config.json and adds .schema-lens/snapshot*.json to .gitignore.
2. Introspect database
export DATABASE_URL="postgres://readonly_user:password@localhost:5432/shop"
npx schema-lens-mcp syncThis generates .schema-lens/schema.md (human and AI readable compact schema) and .schema-lens/snapshot.json.
AI Assistant Setup
Claude Code Setup
Add Schema Lens as an MCP tool directly into Claude Code:
claude mcp add schema-lens -e DATABASE_URL=postgres://readonly@localhost:5432/shop -- npx -y schema-lens-mcp mcpAntigravity / Generic MCP Client Setup
Add to your mcpServers configuration (e.g., claude_desktop_config.json or Antigravity MCP settings):
{
"mcpServers": {
"schema-lens": {
"command": "npx",
"args": ["-y", "schema-lens-mcp", "mcp"],
"env": {
"DATABASE_URL": "postgres://readonly@localhost:5432/shop"
}
}
}
}The "No-MCP" Option
If you prefer not to run an active MCP server:
Run
npx schema-lens-mcp syncin your CI/CD pipeline or git pre-commit hook.Commit
.schema-lens/schema.mdinto your repository.Add this line to your project's
CLAUDE.md,AGENTS.md, or system instructions:"Database schema is in
.schema-lens/schema.md. Read it before writing any query, entity, or migration."
MCP Tool Reference
All tools are annotated with readOnlyHint: true and capped at ~8,000 characters to prevent context overflow.
Tool | Description | Arguments | Output |
| High-level overview of tables, estimated rows, column counts, and outgoing foreign key edges. |
| Plain text overview format + snapshot hash |
| Full column definitions, keys, indexes, and constraints for requested tables, plus direct neighbor tables. |
| Compact detail block + incoming/outgoing neighbor names |
| Breadth-First Search (BFS) over the foreign key graph to discover shortest join path between any two tables. |
| Ready-to-use SQL join condition statements |
| Diffs current database against previous snapshot ( |
| Added/removed/changed tables, columns, indexes, and FKs |
CLI Reference
schema-lens-mcp <command> [options]Commands
init: Scaffold configuration file, update.gitignore, and print MCP config snippets.sync: Connect to database, introspect schema, write.schema-lens/schema.mdandsnapshot.json.check: Verify connection and print dialect, target label, and table count.diff: Print human-readable schema drift since the last snapshot.print [tables...]: Print compact schema directly to stdout (--level overview|detail|full).mcp: Start the Model Context Protocol stdio server.
Global Options
--url <url>: Direct database connection URL (or use environment variable).--env <var>: Name of environment variable containing connection URL (default:DATABASE_URL).-s, --schema <name>: Target schema(s) (e.g.-s public -s billing).-x, --exclude <pattern>: Glob patterns to exclude tables (e.g.-x '_prisma_*' -x 'knex_*').--include-views/--no-views: Toggle view introspection (default: true).--ttl <sec>: Cache freshness time-to-live in seconds (default: 300).--out <dir>: Output directory (default:.schema-lens).--terse: Omit default column expressions and empty constraint sections to maximize token savings.
Supported Databases & Driver Installation
Database drivers are loaded lazily on demand. Install only the driver required for your database:
Database | Connection Scheme | Driver Package | Installation Command |
PostgreSQL |
|
|
|
MySQL / MariaDB |
|
|
|
SQLite |
|
|
|
Configuration Reference (schema-lens.config.json)
{
"envVar": "DATABASE_URL",
"schemas": ["public"],
"exclude": ["_prisma_*", "knex_*", "schema_migrations"],
"includeViews": true,
"ttlSeconds": 300,
"outDir": ".schema-lens",
"terse": false
}Security Note: Never put connection strings containing passwords inside
schema-lens.config.json. UseenvVarto point to an environment variable.
Security & Least Privilege
Schema Lens enforces read-only operations across every layer:
No Data Access: Never executes
SELECT *or reads user row data. Queries only system catalogs (pg_catalog,information_schema,sqlite_schema).Session Read-Only: Establishes read-only transactions on connection (
SET default_transaction_read_only = onin Postgres,SET SESSION TRANSACTION READ ONLYin MySQL,readonly: truein SQLite).Credential Scrubbing: Automatically redacts passwords and secrets from all logs, error messages, metadata files, and MCP tool outputs.
Injection Proof: Table exclusion and schema matching are evaluated purely in memory via JavaScript regular expressions; no user strings are concatenated into catalog queries.
Recommended Read-Only Role SQL
PostgreSQL
-- Create read-only role
CREATE ROLE schema_lens WITH LOGIN PASSWORD 'strong_password';
GRANT CONNECT ON DATABASE shop TO schema_lens;
GRANT USAGE ON SCHEMA public TO schema_lens;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO schema_lens;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO schema_lens;MySQL
-- Create read-only user
CREATE USER 'schema_lens'@'%' IDENTIFIED BY 'strong_password';
GRANT SELECT, SHOW VIEW ON shop.* TO 'schema_lens'@'%';
FLUSH PRIVILEGES;Compact Output Format Syntax
The compact output format uses an intuitive indentation-based syntax:
?: Nullable column (e.g.name text?). Not-null is the default and is omitted.pk: Primary key column (e.g.id uuid pk).uniq: Single-column unique constraint.identity: Auto-incrementing identity column.default <expr>: Default column value expression.fk->table.col: Inline foreign key reference (e.g.user_id uuid fk->users.id).pk: (a, b): Composite primary key.fk: (a, b) -> target(x, y): Composite foreign key.idx: (col1, col2) where cond: Table index and optional partial predicate.check: total >= 0: Table check constraint.
Contributing & Development
# Clone and install dependencies
git clone https://github.com/smitpataliya/schema-lens-mcp.git
cd schema-lens-mcp
npm install
# Run checks and tests
npm run lint
npm run typecheck
npm test
npm run bench
# Build bundle
npm run buildLicense
MIT © 2026 Schema Lens Contributors
This server cannot be deployed
Maintenance
Related MCP Connectors
Text-to-SQL MCP server: read-only queries on PostgreSQL, MySQL and SQL Server with your real schema
Query Postgres, MySQL, SQL Server, Oracle, BigQuery, ClickHouse and Redshift from your AI client.
Serves your design system and coding standards to coding agents, so they stop guessing.
Query your Postgres from ChatGPT or Claude without exposing the database or handing over credentials. Run npx boltschema connect next to your database and it dials out over HTTPS — no inbound firewall rule, no open port, works with localhost and VPC-private databases. Read-only is enforced by a SQL guard, a Postgres READ ONLY transaction, and a scoped role generated for you.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceProvides AI-IDEs with real-time access to PostgreSQL and Supabase database schemas through the Model Context Protocol, enabling smarter code generation in tools like Cursor, Windsurf, and VS Code + Cline.10MIT
- AlicenseAqualityDmaintenanceA Model Context Protocol (MCP) server that gives AI assistants deep visibility into databases, inspecting schemas, detecting index problems, analyzing table bloat, and explaining query plans across PostgreSQL, MySQL, and SQLite.927 npm1MIT
- AlicenseAqualityCmaintenanceExposes SQL Server and Snowflake schema metadata to AI coding agents, enabling schema search, join path discovery, and stored procedure metadata retrieval without live queries.18GPL 3.0
- FlicenseNot gradedqualityDmaintenanceRead-only PostgreSQL schema metadata server for AI coding agents, providing table/column details, relationships, and markdown export via MCP protocol.-