Skip to main content
Glama
SQL Local MCP Server

SQL Local MCP Server

by acoalex
README.md
# SQL Local MCP Server

A Model Context Protocol (MCP) server for executing SQL queries on MySQL and PostgreSQL databases. This allows Claude and other MCP-compatible AI assistants to connect to and interact with your local databases.

## Features

- **Dynamic Database Connections**: Connect to any MySQL or PostgreSQL database
- **READ-ONLY Security**: Only SELECT, SHOW, DESCRIBE, and EXPLAIN queries are allowed
- **Multi-Statement Protection**: Prevents SQL injection
- **Database Exploration**: List tables and describe table structures
- **No Configuration Files**: Database credentials are provided at runtime

## Installation

1. **Clone the repository**
2. **Create and activate a virtual environment**
3. **Install dependencies:**
   ```bash
   pip install -r requirements.txt
   ```

## Usage

### Starting the Server

```bash
./start_server.sh
```
Or:
```bash
python3 mcp_server.py
```

### Available Tools

1. **connect_database**: Connect to a MySQL or PostgreSQL database
2. **execute_query**: Execute READ-ONLY SQL queries
3. **list_tables**: List all tables in a database
4. **describe_table**: Get the structure of a table

### Example: Connecting to a Database

To connect, provide the following details:
- `db_type`: `mysql` or `postgresql`
- `host`: `127.0.0.1`
- `port`: (e.g., 3306 for MySQL, 5432 for PostgreSQL)
- `user`: your_user
- `password`: your_password
- `database`: your_db
- `connection_name`: a unique name for the connection

## Security

- **Read-Only**: Only `SELECT`, `SHOW`, `DESCRIBE`, `EXPLAIN` are allowed.
- **Destructive commands are blocked**: `UPDATE`, `DELETE`, `INSERT`, etc.
- **Multi-statement queries are blocked**.
- **Parameterized queries are supported**.

## Testing

- **Basic tests**: `python3 test_mcp.py`
- **Security tests**: `python3 test_readonly.py`