Skip to main content
Glama
mcp-sql-readonly

mcp-sql-readonly

README.md
# mcp-sql-readonly

A safe, **read-only** [Model Context Protocol](https://modelcontextprotocol.io) server that lets Claude Desktop, Claude Code, Cursor or any MCP client explore and query your SQL database (SQLite or Postgres) without ever being able to change it.

> "Which product category made the most revenue last quarter?" → Claude calls `list_tables`, `describe_table`, writes the SQL, runs it and answers.

## Why it's safe

Two independent layers, so one mistake can't cost you data:

1. **SQL guard:** every query is parsed with [sqlglot](https://github.com/tobymao/sqlglot). Only a single `SELECT` gets through (CTEs and `UNION` included). It rejects `INSERT`, `UPDATE`, `DELETE`, DDL, `PRAGMA`, `ATTACH`, `SELECT ... INTO`, data-modifying CTEs and stacked statements, with a message the model can learn from.
2. **Read-only connection:** SQLite opens with `mode=ro`. Postgres sessions run with `default_transaction_read_only = on`. Even if the guard is bypassed, nothing can be written.

There's also a row cap (default 100 per call, max 1000) that reports `truncated: true`, and a 5-second per-query timeout.

## Tools

| Name | Kind | What it does |
| --- | --- | --- |
| `list_tables` | tool | Tables and views you can query |
| `describe_table` | tool | Columns: name, type, nullable, primary key |
| `run_query` | tool | One read-only SELECT → columns, rows, row_count, truncated, elapsed_ms |
| `schema://overview` | resource | Whole schema as markdown |

All tools are marked `readOnlyHint: true`, so clients can auto-approve them.

## Quick start

```bash
git clone https://github.com/sharmaujwal177-stack/mcp-sql-readonly && cd mcp-sql-readonly
uv venv && uv pip install -e ".[dev]"     # add ,postgres for Postgres
uv run python examples/seed_demo.py       # demo shop database
uv run pytest -q                          # 24 tests
```

**Claude Desktop:** Settings → Developer → Edit Config:

```json
{
  "mcpServers": {
    "shop-db": {
      "command": "/ABSOLUTE/PATH/mcp-sql-readonly/.venv/bin/mcp-sql-readonly",
      "env": { "DATABASE_URL": "sqlite:////ABSOLUTE/PATH/mcp-sql-readonly/examples/demo.sqlite" }
    }
  }
}
```

**Claude Code:**

```bash
claude mcp add shop-db --env DATABASE_URL=sqlite:///$PWD/examples/demo.sqlite -- $PWD/.venv/bin/mcp-sql-readonly
```

**Postgres:** connect as a role with only `SELECT` grants:

```bash
DATABASE_URL=postgresql://readonly_user:pass@host:5432/app mcp-sql-readonly
```

| Env var | Default | Meaning |
| --- | --- | --- |
| `DATABASE_URL` | required | `sqlite:///path.db` or `postgresql://...` |
| `MAX_ROWS` | `1000` | Hard cap on rows per query |
| `QUERY_TIMEOUT_S` | `5` | Per-query timeout |
| `MCP_TRANSPORT` | `stdio` | `http` to serve Streamable HTTP at `http://MCP_HOST:MCP_PORT/mcp` |

## Want more servers?

This server is part of the **[MCP Starter Kit](https://ashtool.gumroad.com/l/mcp-starter-kit)** by ash-tool. The kit adds:

- **GitHub server:** search repos and issues, read PRs and files; writes only when you switch them on
- **Any-REST-API server:** describe endpoints in a short TOML file and each becomes a typed tool, with auth from env vars
- A full setup guide for Claude Desktop, Claude Code and Cursor, plus 43 tests

The server in this repo is free and MIT licensed forever. Issues and PRs welcome.