Skip to main content
Glama
README.md
# mcp-sqlite

A safety-first [Model Context Protocol](https://modelcontextprotocol.io) server
for SQLite. Let an AI agent explore and query a database without letting it drop
tables, run DDL, or smuggle a second statement past you.

## Why another SQLite MCP server

Exposing a database to an agent is useful and dangerous. Most servers either
allow arbitrary SQL or only handle a fixed schema. `mcp-sqlite` sits in between:

- **Read-only by default.** The database is opened read-only and the write tool
  is not registered unless you opt in with `--allow-writes`.
- **Statement-aware guardrails.** Every statement is classified before it runs.
  DDL, `ATTACH`, `VACUUM`, and multiple statements are rejected. `PRAGMA` is
  limited to a safe allowlist.
- **Schema first.** Dedicated tools for tables, columns, foreign keys, and
  indexes, plus a `sqlite://schema` resource, so agents inspect before querying.
- **Bounded results.** Reads are capped (default 100 rows, max 1000) and report
  whether they were truncated.
- **Typed and tested.** TypeScript, Zod-validated tool inputs, and an in-memory
  MCP client integration test.

## Quick start

Requires **Node 24+** (it uses the built-in `node:sqlite` module).

```bash
npm install
npm run build

# Read-only against an existing database
node dist/index.js --db ./data.db

# Allow writes
node dist/index.js --db ./data.db --allow-writes

# Create a new database
node dist/index.js --db ./data.db --create --allow-writes
```

The server speaks MCP over stdio, so stdout is reserved for protocol messages and
logs go to stderr.

## Client configuration

```json
{
  "mcpServers": {
    "sqlite": {
      "command": "node",
      "args": ["/absolute/path/to/mcp-sqlite/dist/index.js", "--db", "/absolute/path/to/data.db"]
    }
  }
}
```

Add `--allow-writes` to the `args` array to enable `run_write`.

## Tools

| Tool | Description |
| --- | --- |
| `list_tables` | List user tables with row counts |
| `describe_table` | Columns, foreign keys, and indexes for one table |
| `run_query` | Execute one read-only statement with optional bound params and a row limit |
| `run_write` | Execute one `INSERT`/`UPDATE`/`DELETE`/`REPLACE` (only with `--allow-writes`) |

Reads accept positional (`?`) or named (`:name`) parameters:

```json
{
  "sql": "SELECT id, title FROM notes WHERE team_id = :team",
  "params": { "team": 1 },
  "limit": 50
}
```

## Resources

| URI | Contents |
| --- | --- |
| `sqlite://schema` | `CREATE` statements for every table, index, view, and trigger |

## Safety model

`classifyStatement` parses the leading keyword after comments and enforces:

- exactly one statement (a semicolon anywhere but the end is rejected)
- reads may be `SELECT`, `WITH`, `VALUES`, or `EXPLAIN`, and may not contain
  write or DDL keywords (this also blocks writes hidden in a CTE)
- writes are limited to `INSERT`, `UPDATE`, `DELETE`, and `REPLACE`
- `PRAGMA` is limited to a read-only allowlist

This is deliberately conservative. It is a guardrail, not a sandbox: run the
server against a database and credentials scoped to what the agent should reach.
For untrusted input, put the database behind a read replica and keep
`--allow-writes` off.

## Development

```bash
npm run typecheck
npm test
npm run build
```

The test suite covers statement classification, introspection, query limits,
parameter binding, write behavior, and a full MCP round-trip using an in-memory
transport.

## License

MIT