arnav1997/mcpsql-lab
3.1
If you are the rightful owner of mcpsql-lab and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
This document provides a comprehensive overview of a Model Context Protocol (MCP) server designed for SQL analysis, featuring built-in tools and extensibility options.
Tools
6
Resources
0
Prompts
0
🚀 Features
Core
- MCP server built with
FastMCP("mcp-sql-analyst"). - Inspector/UI entrypoint via
uv run mcp dev server.py(runsmcp.cli.run_dev(app)).
Built-in Tools
echo(text: str)— echoes text back (sanity test).ping()— health check ("pong").
SQL Tools
list_tables()— lists user tables with:- schema
- table
- size in bytes (
pg_total_relation_size).
describe_table(table: str)— inspects table schema, including:columns(position, name, type, nullable, default)primary_key(PK columns)foreign_keys(relationships with schema/table/column)indexes(non-PK index definitions)table(fully qualified name).
run_sql(sql: str)— executes read-only SQL queries; returns:columnsrowsrowcount.
generate_sql(natural_language_query: str)— generates Postgres SQL from natural language:- Uses Ollama (
OLLAMA_HOST,OLLAMA_MODEL) if available. - Falls back to OpenAI (
OPENAI_API_KEY,OPENAI_MODEL). - Uses schema samples (
information_schema.columns) to guide query generation. - Enforces
SELECT/WITHonly and appendsLIMITautomatically.
- Uses Ollama (
Database Layer
- DSN builder
_build_dsn():- Uses
DATABASE_URLif set, otherwise falls back toPOSTGRES_USER/PASSWORD/HOST/PORT/DB. - Defaults to
postgres/postgres@localhost:5432/pagila.
- Uses
- Read-only session:
- Connects with
default_transaction_read_only=on. - Uses
dict_rowfor dictionary-style row access.
- Connects with
SQL Safety & Sanitization
sanitize_select_only(sql: str)guarantees:- Only one statement (no multiple queries).
- Must start with
SELECTorWITH. - Blocks forbidden keywords (
INSERT,UPDATE,DELETE,CREATE,DROP, etc.). - Blocks dangerous functions (
pg_terminate_backend,pg_sleep, etc.). - Adds a
LIMITif missing (default:SQL_MAX_LIMIT= 500).
- Strips accidental Markdown fences (
```sql). - Safe fallback query (
SELECT 1 AS fallback LIMIT 1) if sanitizer fails.
Extensibility
- Tool registry loader:
register_builtin_tools()imports and registers tools fromtools/(e.g.echo,sysinfo). - Requests library included for future API tools.
- Built for easy extension: add new modules with a
register(app)function.
Configuration (Environment Variables)
- Database:
DATABASE_URL- or
POSTGRES_USER,POSTGRES_PASSWORD,POSTGRES_HOST,POSTGRES_PORT,POSTGRES_DB
- SQL Safety:
SQL_MAX_LIMIT(default: 500)
- LLM Backends:
OLLAMA_HOST,OLLAMA_MODEL(default:llama3.1:8b)OPENAI_API_KEY,OPENAI_MODEL(default:gpt-4o-mini)