Skip to main content
Glama
moaaz01

MCP SQLite Server

by moaaz01
README.md
# šŸ—„ļø MCP SQLite Server

> Query, explore, and manage **SQLite databases** through the **Model Context Protocol**. Connect any MCP-compatible AI client to your databases — Claude Desktop, Cursor, VS Code, and more.

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)

---

## ✨ Features

- **šŸ” 3 Tools**: `query`, `execute`, `list_tables` — full database interaction
- **šŸ“„ 2+ Resources**: `sqlite://tables` (full schema), `sqlite://{table}/schema` (per-table)
- **šŸ’¬ 1 Prompt**: `database_analyst(question)` — guided analysis workflow
- **šŸ”’ Security-First**: Read-only by default, explicit `--allow-write` flag for mutations
- **šŸ“Š WAL Mode**: Write-Ahead Logging for better concurrent access
- **šŸ–„ļø Dual Transport**: stdio (local) and Streamable HTTP (remote)
- **šŸŽ² Sample Data**: Seed script included for instant testing

---

## šŸš€ Quick Start

```bash
# 1. Setup
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# 2. Create sample database
python seed_data.py --db-path /tmp/sample.db

# 3. Start server (read-only mode)
python sqlite_server.py --db-path /tmp/sample.db
```

---

## šŸ› ļø Tools

### `query(sql: str) -> str`
Execute a SELECT query. **Only SELECT and PRAGMA** statements are allowed.

```
Example: query("SELECT name, price FROM products WHERE category = 'Electronics'")
```

### `execute(sql: str) -> str`
Run INSERT, UPDATE, or DELETE. **Requires `--allow-write` flag.**

```
Example (with --allow-write):
  execute("INSERT INTO products (name, price, category, stock) VALUES ('Webcam 4K', 129.99, 'Electronics', 50)")
```

### `list_tables() -> str`
List all tables with row counts.

---

## šŸ”’ Security Model

| Feature | Default | With `--allow-write` |
|---------|---------|---------------------|
| SELECT/PRAGMA queries | āœ… Allowed | āœ… Allowed |
| INSERT/UPDATE/DELETE | āŒ Blocked | āœ… Allowed |
| DROP/ALTER/CREATE | āŒ Blocked | āŒ Blocked |
| Row factory protection | āœ… Enabled | āœ… Enabled |

> āš ļø **Production tip**: Always use read-only mode for public-facing or shared databases. Only enable `--allow-write` in trusted environments.

---

## šŸ“„ Resources

### `sqlite://tables`
Returns complete database schema with column types, nullability, defaults, and primary keys.

### `sqlite://{table}/schema`
Returns schema for a specific table.

---

## šŸ’¬ Prompts

### `database_analyst(query_question: str)`
Creates a structured prompt for analyzing database data.

---

## šŸ”Œ Connecting to Clients

### Claude Desktop

```json
{
  "mcpServers": {
    "sqlite": {
      "command": "python",
      "args": ["/ABSOLUTE/PATH/mcp-sqlite-server/sqlite_server.py", "--db-path", "/tmp/sample.db"]
    }
  }
}
```

### Cursor

Settings → Features → MCP → Add Server:
- **Name**: `sqlite`
- **Type**: `command`
- **Command**: `python /ABSOLUTE/PATH/mcp-sqlite-server/sqlite_server.py --db-path /tmp/sample.db`

---

## šŸ“ Project Structure

```
mcp-sqlite-server/
ā”œā”€ā”€ sqlite_server.py      # Main server (FastMCP)
ā”œā”€ā”€ seed_data.py          # Sample database creator
ā”œā”€ā”€ requirements.txt
ā”œā”€ā”€ setup.sh
ā”œā”€ā”€ README.md
└── .gitignore
```

---

## šŸ“œ License

MIT