mysql-mcp-server

mysql-mcp-server

MCP server that lets clients run SQL (select/insert/update/delete) against a MySQL database, with readonly and readwrite modes.

Category
Visit Server

README

mysql-mcp-server

A Model Context Protocol (MCP) server that lets an MCP client (Claude Desktop, Claude Code, etc.) run SQL against a MySQL database through four tools: select, insert, update, and delete.

The server runs as a uv-managed Python package and communicates with the client over stdio, as a subprocess started by the client.

Requirements

  • Python 3.11+
  • uv
  • A reachable MySQL server

Installation

uv sync

Configuration

The server requires six values, each settable via environment variable and/or CLI flag (CLI flags take priority over environment variables):

Parameter Env var CLI flag Required Default
Mode MYSQL_MODE --mysql-mode yes — (readonly or readwrite)
Host MYSQL_HOST --mysql-host yes
Port MYSQL_PORT --mysql-port no 3306
User MYSQL_USER --mysql-user yes
Password MYSQL_PASSWORD --mysql-password yes
Database MYSQL_DATABASE --mysql-database yes

If a required value is missing, or MYSQL_MODE is not readonly/readwrite, the server prints an error to stderr and exits with status code 1 without starting.

  • readonly mode: only the select tool is allowed. insert/update/delete are rejected with a PERMISSION_DENIED error.
  • readwrite mode: all four tools are allowed.

The mode is fixed for the lifetime of the process; it cannot be changed at runtime.

Security recommendation: readonly mode is an application-level guard, not a substitute for database privileges. Where possible, point readonly mode at a MySQL account that only has SELECT grants.

Is a .env file required? No. The server itself never reads .env files — it only reads CLI flags and real process environment variables (os.environ). How you get values into that environment depends on how you run it:

  • As an MCP server (see Connecting from an MCP client below): the client (Claude Desktop/Code) spawns the server process and injects the env block from its own JSON config directly as environment variables. No .env file is involved or needed.
  • Running the CLI directly for local dev/testing: .env is just a convenience so you don't have to export six variables by hand. Copy .env.example to .env, fill in real values, and load it explicitly — it is not read automatically:
    uv run --env-file .env mysql-mcp-server
    
    .env is git-ignored and must never be committed.

Running

# Environment variables (or use `uv run --env-file .env mysql-mcp-server`, see above)
export MYSQL_MODE=readonly
export MYSQL_HOST=127.0.0.1
export MYSQL_PORT=3306
export MYSQL_USER=app_user
export MYSQL_PASSWORD=secret
export MYSQL_DATABASE=mydb
uv run mysql-mcp-server

# Or, equivalently, via CLI flags
uv run mysql-mcp-server \
  --mysql-mode readonly \
  --mysql-host 127.0.0.1 \
  --mysql-port 3306 \
  --mysql-user app_user \
  --mysql-password secret \
  --mysql-database mydb

Connecting from an MCP client

Claude Desktop / Claude Code

Add an entry to your MCP client's server config (e.g. Claude Desktop's claude_desktop_config.json, or .mcp.json for Claude Code):

{
  "mcpServers": {
    "mysql": {
      "command": "uv",
      "args": [
        "--directory",
        "/absolute/path/to/mysql-mcp-server",
        "run",
        "mysql-mcp-server"
      ],
      "env": {
        "MYSQL_MODE": "readonly",
        "MYSQL_HOST": "127.0.0.1",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "app_user",
        "MYSQL_PASSWORD": "secret",
        "MYSQL_DATABASE": "mydb"
      }
    }
  }
}

Restart the client after editing the config. The select, insert, update, and delete tools (subject to MYSQL_MODE) should then be available to the model.

Tools

All four tools take {"query": string, "params"?: array} and always use %s parameter-binding placeholders in query — never string-format user input into a query.

Tool Allowed in Query must start with Success data shape
select any mode SELECT / WITH {rows, row_count, truncated} (capped at 1000 rows)
insert readwrite only INSERT {affected_rows, last_insert_id}
update readwrite only UPDATE {affected_rows} (+ warning if no WHERE)
delete readwrite only DELETE {affected_rows} (+ warning if no WHERE)

Every tool call returns one of:

{ "success": true, "data": { ... } }
{ "success": false, "error": { "code": "...", "message": "..." } }

Error codes: PERMISSION_DENIED, INVALID_QUERY_TYPE, MULTI_STATEMENT_NOT_ALLOWED, DB_CONNECTION_ERROR, DB_EXECUTION_ERROR, INTERNAL_ERROR.

Multi-statement queries (;-separated) and any DDL/privilege statement (DROP, TRUNCATE, ALTER, GRANT, CREATE USER, ...) are always rejected, since only the four whitelisted statement types above are ever accepted.

Development

uv sync
uv run ruff format .
uv run ruff check .
uv run pytest -v
uv run uv build   # packaging check

Troubleshooting

  • Server exits immediately with status 1: a required MYSQL_* value is missing or MYSQL_MODE is invalid — check stderr for which one.
  • DB_CONNECTION_ERROR: MySQL is unreachable, or the credentials are wrong. The server keeps running and will retry the connection on the next tool call.
  • PERMISSION_DENIED on insert/update/delete: the server is running in readonly mode; restart it with MYSQL_MODE=readwrite if writes are intended.

Version history

  • 0.1.0 — Initial release: select/insert/update/delete tools, readonly/ readwrite mode policy, stdio MCP transport, automatic reconnect-and-retry on lost connections.

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured
E2B

E2B

Using MCP to run code via e2b.

Official
Featured