Skip to content

eduartedev/mysql-mcp-server

v1.2.0MIT

MCP server for MySQL, MariaDB, and PostgreSQL with validated SQL, read-only defaults, allowlists, and PII masking.

mysql-mcp-server

MCP server for querying and controlled modification of MySQL/MariaDB or PostgreSQL. Single binary; the engine is chosen with DB_ENGINE or, if missing, from DB_PORT (5432 → PostgreSQL, 3306 → MySQL). Built for Cursor or any other MCP client.

Requirements

  • Node.js >= 18
  • Reachable MySQL/MariaDB or PostgreSQL (local, Laragon, RDS, etc.)

Installation

npm install

Configuration

Connection via environment variables. On startup a .env in the working directory is loaded (not committed to Git). Keys DB_*, MYSQL_*, and POSTGRES_* are accepted; the process does not ship them.

Engine

VariableDefaultDescription
DB_ENGINEautodetect / mysqlmysql or postgres. If missing, DB_PORT=5432 → postgres; DB_PORT=3306 → mysql
DB_HOSTlocalhostHost (wins over MYSQL_HOST / POSTGRES_HOST)
DB_PORT3306 or 5432 by enginePort
DB_USERroot / postgresUsername
DB_PASSWORD(empty)Password
DB_DATABASE(optional)Connection database for MySQL or PostgreSQL
DB_SCHEMApublic (Postgres only)Default schema for search_path and allowlist
DB_ACCESS_MODEreadonlyOnly readwrite enables writes
DB_ALLOWED_DATABASES(empty)Allowlist name, name:ro, name:rw. In Postgres these are schemas
DB_PII_COLUMNS(empty)Extra columns to mask
DB_SSL(empty)Only required enables TLS
DB_SSL_CA / CERT / KEY(optional)PEM file paths on disk
DB_SSL_REJECT_UNAUTHORIZEDtrueOnly false disables verify

MYSQL_* and POSTGRES_* remain as fallbacks (DB_* wins when both are set).

MySQL / MariaDB (MYSQL_* aliases)

VariableDefaultDescription
MYSQL_HOSTlocalhostMySQL server host
MYSQL_PORT3306Port
MYSQL_USERrootUsername
MYSQL_PASSWORD(empty)Password
MYSQL_DATABASE(optional)Default database
MYSQL_ACCESS_MODEreadonlyOnly the exact value readwrite enables INSERT, UPDATE, and DELETE. Any other value, or an unset variable, is read-only
MYSQL_ALLOWED_DATABASES(empty)Empty or unset: the tool may use MYSQL_DATABASE but cannot switch to another database. Comma-separated list of name, name:ro, or name:rw
MYSQL_PII_COLUMNS(empty)Extra column names, comma-separated. email, correo, phone, telefono, password, contrasena, ssn, nss, and token are already masked. A token with spaces or punctuation is ignored; the rest still apply.
MYSQL_SSL(empty)Only the exact value required enables TLS. Any other non-empty value is an error. Empty or unset: no TLS
MYSQL_SSL_CA(optional)Path to a PEM file with the CA. With MYSQL_SSL=required, the file must exist and be readable
MYSQL_SSL_CERT(optional)Client certificate path (mTLS). Requires MYSQL_SSL_KEY as well
MYSQL_SSL_KEY(optional)Client private key path (mTLS). Requires MYSQL_SSL_CERT as well
MYSQL_SSL_REJECT_UNAUTHORIZEDtrueOnly the exact value false disables server certificate verification. Any other value keeps it enabled

MYSQL_SSL* variables accept file paths to PEM files on disk only, never inline certificate content. They are read once when the connection pool is created (on the first query after the server starts); changing them requires restarting the MCP. rejectUnauthorized defaults to true when TLS is enabled. MariaDB uses the same mysql2 client, so configuration is identical.

PostgreSQL

Tools: postgres_query, postgres_catalog. The schema argument (not database) adjusts search_path. RDS usually needs DB_SSL=required and DB_SSL_CA pointing at the AWS PEM bundle. If the port is not 5432, set DB_ENGINE=postgres.

Example Postgres .env:

DB_HOST=your-instance.region.rds.amazonaws.com
DB_PORT=5432
DB_USER=app_readonly
DB_PASSWORD=your_password
DB_DATABASE=app
DB_SCHEMA=public
DB_ACCESS_MODE=readonly
DB_ALLOWED_DATABASES=public,reporting:ro
DB_SSL=required
DB_SSL_CA=C:/path/global-bundle.pem

Languages: English · Español

Example .env:

MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password
MYSQL_DATABASE=my_database
MYSQL_ACCESS_MODE=readonly
MYSQL_ALLOWED_DATABASES=
MYSQL_PII_COLUMNS=

Using in Cursor

Without cloning the repository (npm + npx)

With Node.js >= 18 installed, in Cursor Settings → MCP:

{
  "mcpServers": {
    "mysql": {
      "command": "npx",
      "args": ["-y", "@eduartedev/mysql-mcp-server"],
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_USER": "root",
        "MYSQL_PASSWORD": "",
        "MYSQL_DATABASE": "your_database",
        "MYSQL_ACCESS_MODE": "readonly",
        "MYSQL_ALLOWED_DATABASES": "",
        "MYSQL_PII_COLUMNS": ""
      }
    }
  }
}

The first time, npx downloads the package from the registry; you do not need the repo on disk. Restart Cursor or reload MCP after saving.

Local development (clone the repo)

  1. Build (optional if you use tsx in step 2):

    npm run build
    
  2. Add the MCP in Cursor configuration:

    • Open Cursor Settings → MCP (or your MCP config file).
    • Add a stdio server, for example:

    Option A – with tsx (no build):

    {
      "mcpServers": {
        "mysql": {
          "command": "npx",
          "args": ["tsx", "c:/laragon/www/mysql-mcp-serve/src/index.ts"],
          "env": {
            "MYSQL_HOST": "localhost",
            "MYSQL_USER": "root",
            "MYSQL_PASSWORD": "",
            "MYSQL_DATABASE": "your_database",
            "MYSQL_ACCESS_MODE": "readonly",
            "MYSQL_ALLOWED_DATABASES": "",
            "MYSQL_PII_COLUMNS": "ssn"
          }
        }
      }
    }
    

    Option B – with build (node):

    {
      "mcpServers": {
        "mysql": {
          "command": "node",
          "args": ["c:/laragon/www/mysql-mcp-serve/dist/index.js"],
          "env": {
            "MYSQL_HOST": "localhost",
            "MYSQL_USER": "root",
            "MYSQL_PASSWORD": "",
            "MYSQL_DATABASE": "your_database",
            "MYSQL_ACCESS_MODE": "readonly",
            "MYSQL_ALLOWED_DATABASES": "",
            "MYSQL_PII_COLUMNS": "ssn"
          }
        }
      }
    }
    

    Adjust the path (c:/laragon/www/mysql-mcp-serve) to the real project path on your machine.

  3. Restart Cursor or reload MCP so it picks up the server. MYSQL_ACCESS_MODE, MYSQL_ALLOWED_DATABASES, and MYSQL_PII_COLUMNS are read at startup; changes do not apply until you restart.

Available tools

Depending on the active engine:

  • mysql_query / mysql_catalog (MySQL/MariaDB)
  • postgres_query / postgres_catalog (PostgreSQL)

mysql_query

  • query: query text.
  • database: (optional) database to use; defaults to MYSQL_DATABASE.
  • limit: (optional) rows per page, default 20, max 100.
  • offset: (optional) rows to skip before the page, default 0, max 100000.
  • response_format: (optional) markdown (default) or json.

mysql_catalog

Reads view, trigger, function, and procedure definitions from information_schema.

  • kind: view, trigger, function, or procedure.
  • name: (optional) object name; letters, numbers, and underscore only. If omitted, lists all objects of that type. The server passes it as a parameter; it is not concatenated into SQL.
  • database: (optional) database to query; must be allowed by MYSQL_ALLOWED_DATABASES. Defaults to MYSQL_DATABASE.
  • limit, offset, and response_format: same as mysql_query.

A SELECT without its own LIMIT is executed with LIMIT offset, count. If the query already has LIMIT, ends with a line comment, or uses FOR UPDATE / LOCK IN SHARE MODE, MySQL returns at most offset + limit + 1 rows and pagination is applied here. Other commands are paginated in memory.

Structured response fields (structuredContent):

FieldDescription
totalCountFor a SELECT with LIMIT added here: offset plus rows MySQL returned (includes one extra row when another page exists). Otherwise, rows returned before slicing the page
countRows on this page
offsetApplied offset
limitApplied limit
hasMoretrue if rows remain after this page
nextOffsetOffset for the next page, or null if none
truncatedtrue if text exceeded 25000 characters and was cut
rowsPage rows (plain objects)

Text responses longer than 25000 characters are truncated and marked truncated.

Allowed commands

TypeCommands
ReadSELECT, WITH, SHOW, DESCRIBE / DESC, EXPLAIN, ANALYZE, CHECK, CHECKSUM
WriteINSERT · UPDATE / DELETE (require WHERE)

Allowed SHOW examples: SHOW TABLES, SHOW DATABASES, SHOW COLUMNS, SHOW INDEX, SHOW CREATE TABLE, SHOW TABLE STATUS, SHOW STATUS, SHOW CHARACTER SET, SHOW COLLATION, SHOW ENGINES, SHOW WARNINGS, SHOW ERRORS, SHOW OPEN TABLES, SHOW TRIGGERS, SHOW FUNCTION STATUS, SHOW PROCEDURE STATUS, SHOW CREATE VIEW, SHOW CREATE TRIGGER, SHOW CREATE FUNCTION, SHOW CREATE PROCEDURE.

Security restrictions

  • The server is read-only by default. Writes (INSERT, UPDATE, DELETE) require MYSQL_ACCESS_MODE=readwrite (or DB_ACCESS_MODE=readwrite); the value is read at startup, so changing it requires restarting the MCP.
  • A database marked :ro in MYSQL_ALLOWED_DATABASES / DB_ALLOWED_DATABASES rejects writes even with readwrite access. That includes a qualified name (analytics.table) used from another database. Marking a database :rw does not override a read-only process. The list is read at startup. In Postgres the allowlist applies to schemas.
  • A qualified name (other_db.table, with spaces, backticks, or double quotes) is checked against the allowlist. Same for SHOW CREATE, DESCRIBE, SHOW TABLES FROM, ANALYZE TABLE, CHECK TABLE, and CHECKSUM TABLE.
  • mysql_catalog / postgres_catalog only read catalog metadata. They do not run CALL or CREATE. Object names are not concatenated into SQL.
  • Columns email, correo, phone, telefono, password, contrasena, ssn, nss, and token, plus those in DB_PII_COLUMNS / MYSQL_PII_COLUMNS, are returned as ***. This applies to text and structuredContent. A column like notes is not inspected even if the value looks like an email.
  • When writes are enabled, UPDATE and DELETE must include a WHERE clause.
  • Always-true WHERE clauses are rejected (WHERE 1=1, WHERE (1=1), WHERE TRUE, WHERE 1, WHERE 'a'='a', OR 1=1). Before MySQL UPDATE or DELETE, the connection enables sql_safe_updates.
  • Multiple statements in a single query are not allowed.
  • Destructive commands are blocked: DROP, TRUNCATE, ALTER, CREATE, REPLACE, RENAME, GRANT, REVOKE, LOAD DATA, FLUSH, RESET, PREPARE, etc.
  • Sensitive SHOW variants are blocked: SHOW GRANTS, SHOW VARIABLES, SHOW PROCESSLIST, SHOW MASTER/SLAVE/REPLICA STATUS, SHOW BINARY LOGS, etc.
  • Dangerous functions are blocked: SLEEP(), BENCHMARK(), LOAD_FILE(), INTO OUTFILE, INTO DUMPFILE.

Cursor plugin (Marketplace & cursor.directory)

This repo ships plugin manifests (.cursor-plugin/, mcp.json, .mcp.json, skills/). Step-by-step guide: PUBLISHING.es.md (Spanish).

Project commands

CommandDescription
npm installInstall dependencies
npm run buildCompile TypeScript
npm startRun (uses dist/)
npm run devRun with tsx (no build)
npm testRun unit tests

License

MIT