Skip to main content
Glama
MCP SQL Connector

MCP SQL Connector

by sruthir28
README.md
# MCP SQL Connector

An MCP server that lets Claude query SQLite databases. Ask questions in natural language and Claude translates them into SQL queries.

## Tools

| Tool | What it does |
|------|-------------|
| `list_tables` | Show all tables in the database |
| `describe_table` | Show columns and types for a table |
| `query` | Run a read-only SQL SELECT query |
| `summarize_table` | Get row count + sample rows |

## Safety

- Database is opened in **read-only mode** (`mode=ro`) at the driver level
- Only `SELECT` and `WITH` (CTE) queries are allowed
- Dangerous keywords (`INSERT`, `UPDATE`, `DELETE`, `DROP`, `ALTER`, `ATTACH`, `LOAD_EXTENSION`, etc.) are blocked
- Table names are validated against a strict alphanumeric pattern before use
- SQL comments are stripped before safety checks to prevent bypass attempts

## Setup

### Prerequisites
- Python 3.10+
- A SQLite database file

### Install

```bash
git clone https://github.com/sruthir28/mcp-sql-connector.git
cd mcp-sql-connector
python -m venv .venv
source .venv/bin/activate
pip install -e .
```

### Create sample data (optional)

```bash
python setup_db.py
```

### Configure in Claude Code

Add to your project's `.mcp.json`:

```json
{
  "mcpServers": {
    "sql-connector": {
      "command": "/path/to/mcp-sql-connector/.venv/bin/python",
      "args": ["/path/to/mcp-sql-connector/server.py"],
      "env": {
        "MCP_DB_PATH": "/path/to/your/database.db"
      }
    }
  }
}
```

### Configure in Claude Desktop

Add to `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "sql-connector": {
      "command": "/path/to/mcp-sql-connector/.venv/bin/python",
      "args": ["/path/to/mcp-sql-connector/server.py"],
      "env": {
        "MCP_DB_PATH": "/path/to/your/database.db"
      }
    }
  }
}
```

## Usage Examples

Once connected, you can ask Claude things like:
- "What tables are in the database?"
- "Show me all employees in Engineering"
- "What's the total budget across all active projects?"
- "Which tasks are high priority and still in progress?"

Claude will use the MCP tools to query the database and return results.

## Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `MCP_DB_PATH` | `sample.db` | Path to the SQLite database file |
| `MCP_MAX_ROWS` | `100` | Maximum rows returned per query |
| `MCP_QUERY_TIMEOUT_MS` | `5000` | Query timeout in milliseconds |

## Error Responses

All tools return errors in a consistent JSON format:

```json
{"error": "Description of what went wrong"}
```

Common errors:
- **Table not found**: The table name doesn't exist in the database
- **Invalid table name**: Table name contains special characters (only letters, numbers, underscores allowed)
- **Query rejected**: The SQL contains disallowed keywords or doesn't start with SELECT
- **Database not found**: The `MCP_DB_PATH` doesn't point to a valid file (server won't start)

## License

MIT