mysqldb-mcp-server

mcp
Security Audit
Pass
Health Pass
  • License — License: MIT
  • Description — Repository has a description
  • Active repo — Last push 0 days ago
  • Community trust — 10 GitHub stars
Code Pass
  • Code scan — Scanned 12 files during light audit, no dangerous patterns found
Permissions Pass
  • Permissions — No dangerous permissions requested

No AI report is available for this listing yet.

SUMMARY

Lets AI assistants run SQL against a MySQL database over MCP.

README.md

MySQL MCP Server

An MCP server that lets AI assistants explore and query a
MySQL database. It is read-only by default: writing needs an
explicit opt-in.

It uses the official MySQL Connector/Python
and runs over stdio. It is tested with MySQL 8.4 and 9.7.

Tools

Tool Available What it does
mysql_query always Runs one reading statement (SELECT, WITH, TABLE, VALUES, SHOW, DESC, DESCRIBE, EXPLAIN) in a read-only transaction. Returns columns, rows, row_count, a truncated flag and MySQL's warnings.
mysql_execute only with MYSQL_ALLOW_WRITE=true Runs one statement that changes data or schema, with autocommit. Returns rows_affected (for a CALL, that of the procedure's last statement), last_insert_id and warnings; rows it returns are discarded.

Both tools take a single sql argument holding exactly one statement; the server disables
multi-statements, so SELECT 1; DROP TABLE t is a syntax error. Every call opens its own
connection, so USE, SET, user variables, temporary tables, locks and transactions do not
carry over to the next call. Use schema.table names; MYSQL_DATABASE only sets the default.

A call gets MYSQL_QUERY_TIMEOUT seconds, including any wait for a free slot. At most 16
statements run at once, counting those still being stopped; a call that finds no free slot in
time gets a "busy" error. At the deadline the server closes the statement's connection, waits
half a second for MySQL to notice, and then kills the session, if it still exists, with KILL
from a second connection, after checking that this connection reached the same server
(@@server_uuid), as it may not behind a proxy. Stopping adds at most about 4 seconds;
if MySQL does not answer in that time, the error says the statement may still be running.
Killing only after closing matters: MySQL 8.4 and 9.7 can corrupt other clients' TLS
connections when a session is killed while MySQL is blocked writing to it.

mysql_query streams rows and stops at MYSQL_MAX_ROWS rows or 24 KB of UTF-8 JSON (column
names included), whichever comes first; truncated is then true. The result is returned at
once, and the statement is stopped the same way as at the deadline, in the background but
still holding its slot, so a statement that goes on computing after its first rows does not
keep running. A call the client cancels keeps its slot until its session is over too. A
response carries the rows twice, as structured content and as the same JSON in text, which
keeps it well within the roughly 25,000 tokens that clients such as Claude Code accept from a
tool.

When the server shuts down, whether stdin closes or it gets SIGTERM, SIGINT or SIGHUP, it
refuses new calls, stops the statements still running and answers their calls, waiting at
most about 6 seconds for that. A second signal exits at once, which may leave a statement
running. A client that stops reading the server's output while it shuts down may get its last
response cut off.

Memory is bounded: mysql_query reads at most 256 KB of column definitions and 96 KB of rows
from MySQL per call, and mysql_execute at most 1 MB per row it discards, so 16 calls at once
refusing 5 MB rows over TLS keep the server process around 150 MB (measured with 1,280 such
calls). MySQL itself may still use a lot of memory to build large rows.

MySQL counts every connection that it accepts but that does not complete toward
max_connect_errors (100 by default), and then refuses every connection from that host until
FLUSH HOSTS. So when a connection cannot be made, the server opens none for 10 seconds, and
then one at a time until one succeeds; calls in that time fail at once. It opens no connection
to stop a statement that was never sent.

Values are JSON. DECIMAL and integers beyond 2^53 are strings, DATETIME and TIMESTAMP are
written like 2024-02-29T13:14:15.123, DATE and TIME are as MySQL writes them (including zero
dates and -01:30:00), JSON columns are JSON, BIT is an integer, ENUM and SET are their
text (a,b), and binary strings (BINARY, VARBINARY, BLOB, GEOMETRY, VECTOR) are
0x-prefixed hex. DATETIME and TIMESTAMP carry no time zone; TIMESTAMP values are in the
session's time zone, which is the server's default (@@time_zone).

Installation

You need uv. The server is started by your MCP client; there is
nothing to run by hand.

Claude Code

claude mcp add mysql \
  --env MYSQL_HOST=localhost \
  --env MYSQL_USER=mcp \
  --env MYSQL_PASSWORD=your-password \
  --env MYSQL_DATABASE=shop \
  -- uvx mysqldb-mcp-server

Claude Desktop and other clients

Add this to the client's MCP configuration (for Claude Desktop, claude_desktop_config.json):

{
  "mcpServers": {
    "mysql": {
      "command": "uvx",
      "args": ["mysqldb-mcp-server"],
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_USER": "mcp",
        "MYSQL_PASSWORD": "your-password",
        "MYSQL_DATABASE": "shop"
      }
    }
  }
}

Docker

docker build -t mysqldb-mcp-server .
docker run -i --rm -e MYSQL_HOST=host.docker.internal -e MYSQL_USER=mcp \
  -e MYSQL_PASSWORD=your-password mysqldb-mcp-server

Use docker run -i ... as the command of the configuration above. host.docker.internal
reaches a MySQL on your machine with Docker Desktop; on Linux, also pass
--add-host=host.docker.internal:host-gateway.

Configuration

The server is configured only through environment variables. It does not read .env files.
Invalid values stop the server at startup with a message saying which one.

Variable Default Description
MYSQL_HOST required Host name or IP address.
MYSQL_PORT 3306 TCP port.
MYSQL_USER required User name.
MYSQL_PASSWORD empty Password.
MYSQL_DATABASE none Default schema for unqualified table names.
MYSQL_SSL_MODE PREFERRED, or VERIFY_CA when MYSQL_SSL_CA is set DISABLED, PREFERRED, REQUIRED, VERIFY_CA or VERIFY_IDENTITY, as in the mysql client. Use VERIFY_IDENTITY for any server reached over a network.
MYSQL_SSL_CA none CA certificate file that VERIFY_CA and VERIFY_IDENTITY trust in addition to the system's CAs, for example your cloud provider's bundle. Other modes refuse it.
MYSQL_ALLOW_WRITE false Register the mysql_execute tool, and allow an account that may write.
MYSQL_QUERY_TIMEOUT 30 Seconds a call may take before its statement is stopped.
MYSQL_MAX_ROWS 1000 Most rows mysql_query returns.

With DISABLED, accounts using caching_sha2_password, MySQL's default, can only log in
after a TLS login has cached their password on the server (it forgets after a restart or
FLUSH PRIVILEGES), because Connector/Python does not do the RSA key exchange such a login
needs; the error then says so. REQUIRED refuses a server without TLS before sending any
credentials. The certificates MySQL
generates for itself at first start do not pass verification on Python 3.13 and later,
including the Docker image ("Missing Authority Key Identifier"); give the server a certificate
from a proper CA to use VERIFY_CA or VERIFY_IDENTITY.

Security

The MySQL account's privileges are the real boundary: everything the account may read, the
assistant can read, and query results are sent to your AI provider. Use a dedicated account
with read privileges on the schemas the assistant should see and nothing else:

CREATE USER 'mcp'@'%' IDENTIFIED BY 'your-password';
GRANT SELECT, SHOW VIEW ON shop.* TO 'mcp'@'%';

Unless MYSQL_ALLOW_WRITE=true, the server enforces this: it refuses to start, and
mysql_query refuses to run, if the account, with the roles active in its session, holds any
privilege other than SELECT, SHOW VIEW, SHOW DATABASES, PROCESS and USAGE, or if its
grants cannot be read. The check runs at startup and again on every call, so roles activated
or grants added later are caught too. MySQL must therefore be reachable when the server starts.
A read-only transaction alone cannot stop what a privileged account can do through a SELECT:
built-in functions that change replication or InnoDB settings, and stored functions that run
SET GLOBAL, work inside one.

What the server cannot check: views and routines with SQL SECURITY DEFINER (the default) run
with their owner's privileges. A read-only account that may select from a view, or execute a
routine, owned by a more privileged account can do through it whatever the owner allows,
including changing server settings. Review such objects in the schemas you grant.

Reading has side effects worth knowing. PROCESS, or SELECT on performance_schema (which
SELECT ON *.* includes), shows other sessions' statements. A long read holds metadata locks
on its tables until the call ends; an ALTER TABLE on them waits for it, and queries that
arrive after the ALTER wait behind it. The account needs two connections per running call,
one for the statement and one to stop it, so keep max_user_connections above twice the calls
you expect at once.

mysql_query adds layers on top of the account's privileges:

  • It refuses any statement that does not start with a reading keyword, any statement with an
    executable comment (/*! ... */), and any statement containing the words OUTFILE or
    DUMPFILE, even in a string.
  • It runs the statement with transaction_read_only on and inside START TRANSACTION READ ONLY,
    so MySQL refuses writes to tables, including WITH ... DELETE, SELECT ... FOR UPDATE and
    stored functions that write. Shared locking reads (FOR SHARE, LOCK IN SHARE MODE) run and
    hold their locks until the call ends.
  • It disables multi-statements and LOAD DATA LOCAL, and closes the connection after every
    call.

A statement may still load the server until the timeout stops it. Optimizer hints can lift
MySQL's max_execution_time, which is why the server enforces the timeout itself.

Writing is opt-in. With MYSQL_ALLOW_WRITE=true the server skips the account check and also
offers mysql_execute, which runs with all privileges of the configured account, including
DROP. mysql_query then still refuses writes to tables, but functions the account may run
can change the server's state, so it is no longer marked read-only. Enable writing only with an
account whose privileges match what you are willing to let the assistant change, and keep your
client's tool-call confirmation on. The password is only used to connect; it is never included
in tool results or error messages.

Development

uv sync

The tests run against a real MySQL, with an account that has all privileges, as root does
(they create a schema, a routine, roles and users). Start one, for example with Docker:

docker run -d --rm -p 3306:3306 -e MYSQL_ROOT_PASSWORD=ci mysql:8.4

then point the tests at it with the same variables the server uses (the host defaults to
127.0.0.1 and the user to root):

export MYSQL_PASSWORD=ci
uv run pytest
uv run ruff check . && uv run ruff format --check .
uv run pyright

The tests of certificate verification run when MYSQL_TEST_SSL_CA names the CA that signed
the server's certificate for 127.0.0.1; CI sets this up, as .github/workflows/ci.yml shows.

To release, set the new version in pyproject.toml and both places in server.json, date the
CHANGELOG entry, and push a vX.Y.Z tag; the release workflow publishes to PyPI and the MCP
Registry and creates the GitHub release.

License

MIT

Reviews (0)

No results found