Metadata-Version: 2.4
Name: mcp-sql-only
Version: 0.1.1
Summary: Standalone MCP server exposing a single sql_generator tool for MySQL-backed natural-language-to-SQL
Author-email: Ramkumar <ramkumar.b@finastra.com>
License: MIT
Keywords: mcp,sql,mysql,llm,natural-language-to-sql,ai-agent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Database
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: mcp>=1.0.0
Requires-Dist: python-dotenv>=1.0.0
Requires-Dist: openai>=1.0.0
Requires-Dist: pydantic>=2.0.0
Requires-Dist: sqlparse>=0.5.0
Requires-Dist: PyMySQL>=1.1.1

# mcp-sql-only

A standalone MCP server that exposes a single `sql_generator` tool.  
Give it a natural-language question and your MySQL connection details — it introspects the schema at runtime, generates a validated `SELECT` query via Azure OpenAI, and optionally executes it.

## How it works

```
question + MySQL creds
        │
        ▼
  schema introspection  ──►  Azure OpenAI  ──►  SQL validator  ──►  (execute)
```

1. Connects to your MySQL database and reads the live schema.
2. Sends the schema + question to Azure OpenAI to generate SQL.
3. Validates the SQL (SELECT-only policy, table/column whitelist, no dangerous keywords).
4. Returns the query — or executes it and returns rows.

## Required environment variables

Set these before starting the server (in your shell or a `.env` file in the working directory):

```env
AZURE_OPENAI_API_KEY=your-key
AZURE_OPENAI_ENDPOINT=https://<resource>.openai.azure.com/
AZURE_DEPLOYMENT=your-deployment-name
AZURE_OPENAI_API_VERSION=2024-02-15-preview
```

## Quickstart — run without installing (recommended)

```bash
uvx mcp-sql-only
```

`uvx` downloads the package from PyPI into a temporary isolated environment and runs it immediately. No global install, no dependency conflicts.

## Plug into any MCP-compatible agent

```python
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_agent

client = MultiServerMCPClient(
    {
        "sql": {
            "transport": "stdio",
            "command": "uvx",
            "args": ["mcp-sql-only"],
        }
    }
)
tools = await client.get_tools()

agent = create_agent(
    model=model,
    tools=tools,
)
```

## Tool — `sql_generator`

| Parameter | Required | Default | Description |
|---|---|---|---|
| `question` | ✅ | — | Natural-language question |
| `host` | ✅ | — | MySQL host |
| `username` | ✅ | — | MySQL user |
| `password` | ✅ | — | MySQL password |
| `database_name` | ✅ | — | Target database |
| `port` | ❌ | `3306` | MySQL port |
| `table` | ❌ | `all` | Restrict query to a single table |
| `max_retries` | ❌ | `0` | Retry attempts if SQL validation fails |
| `debug` | ❌ | `false` | Include internal workflow logs in response |

## Security

- **SELECT-only** — `DROP`, `DELETE`, `UPDATE`, `INSERT`, `TRUNCATE` and other destructive statements are blocked before execution.
- **Schema whitelist** — generated SQL is validated against the live schema; unknown tables and columns are rejected.
- **No credential logging** — passwords are masked as `***` in all server logs.
- **Credentials are never stored** — connection details are passed per-call and held only for the duration of that request.

## Project structure

```
server.py           ← MCP server entry point
sql_engine/
  generator.py      ← LLM prompt + SQL generation
  validator.py      ← SELECT-only + whitelist validation
  llm_providers.py  ← Azure OpenAI provider
  tool.py           ← orchestrates generate → validate → execute
  models.py         ← Pydantic input/output contracts
pyproject.toml      ← package metadata + entry point
```


