Skip to main content
Glama
MySQL MCP Server

MySQL MCP Server

by iamdylanngo
README.md
# MySQL Mate

Let Claude (or any MCP client) read your local MySQL database so you stop switching between your editor, a SQL client and the terminal. **Reads stay read-only, and nothing in your database changes unless you approve it.**

---

## The problem

Debugging on a local workstation usually means jumping back and forth:

1. Read the code to work out which tables and columns a feature touches.
2. Open a SQL client, find the right schema, and write a query by hand.
3. Copy the results back into your editor or chat, compare them with what the code expects, and repeat.

Every round trip takes minutes, and you lose context each time you switch tools. An AI assistant can read your code, but it can't see your data, so it ends up guessing about schemas and row contents.

The obvious fix is to give the AI direct database access. That raises a new worry: **what stops it from running an `UPDATE`, `DELETE` or `DROP TABLE` on its own?**

## The solution

This MCP server connects Claude to your MySQL database with guardrails built in:

- **Claude can look on its own.** It lists tables, describes schemas and runs read queries while it reads your code, so it checks its assumptions against real data in the same conversation.
- **Reads are guaranteed read-only.** The `query` tool accepts only read statements, and runs each one inside a MySQL `READ ONLY` transaction. If something slips past the check, MySQL itself rejects the write.
- **Every write needs your explicit approval.** The `mutate` tool shows you the exact SQL, its parameters and an estimated row count, and it runs only after you approve. This is enforced by the server, not just by a client setting.
- **Schema changes are off by default.** You decide whether `DROP`, `ALTER` or `TRUNCATE` are possible at all.

The result is that you can let the AI investigate freely, while you stay in control of every change.

---

## How to use

You need Node.js 18+ and a running MySQL database.

### Claude Desktop

1. Download [**mysql-mate.mcpb**](https://github.com/iamdylanngo/mysql-mate/releases/latest/download/mysql-mate.mcpb) (always the latest release).
2. Double-click it, then fill in your database host, user, password and database name.

### Claude Code

```bash
claude mcp add mysql \
  -e DB_HOST=127.0.0.1 -e DB_USER=claude_reader -e DB_PASSWORD=your_password -e DB_NAME=your_database \
  -- npx -y mysql-mate
```

### Other MCP clients (Cursor, VS Code, Claude Desktop JSON config…)

```json
{
  "mcpServers": {
    "mysql": {
      "command": "npx",
      "args": ["-y", "mysql-mate"],
      "env": {
        "DB_HOST": "127.0.0.1",
        "DB_USER": "claude_reader",
        "DB_PASSWORD": "your_password",
        "DB_NAME": "your_database"
      }
    }
  }
}
```

### From source

```bash
git clone https://github.com/iamdylanngo/mysql-mate && cd mysql-mate
npm install && npm run build
claude mcp add mysql -e DB_HOST=127.0.0.1 -e DB_USER=claude_reader -e DB_PASSWORD=your_password -e DB_NAME=your_database \
  -- node "$(pwd)/build/index.js"
```

Optional settings (read-only mode, schema changes, audit log) are listed under [Configuration](#configuration).

### Example prompts

- "Read `src/orders/service.ts` and check whether the `orders` table actually has the columns it queries."
- "Why does user 42 see an empty cart? Look at the code and the `cart_items` rows for that user."
- "Find orders stuck in `pending` for more than a day, then propose a fix."
- "Mark order 1001 as shipped." (Claude asks you to approve the `UPDATE` before it runs.)

### Tools

| Tool | What it does | Changes data? |
|------|--------------|---------------|
| `select_mysql_version` | Returns the MySQL server version | No |
| `list_tables` | Lists the tables in the connected database | No |
| `describe_table` | Shows columns, types and constraints for a table | No |
| `query` | Runs `SELECT`, `SHOW`, `DESCRIBE`, `EXPLAIN` or `WITH … SELECT`, with optional `?` params | No, enforced read-only |
| `mutate` | Runs `INSERT`, `UPDATE`, `DELETE` or `REPLACE` (and DDL if enabled) | Yes, **only after your approval** |

---

## Why it's safe to use

Each layer works on its own, so one failing doesn't open the door.

| Risk | What stops it |
|------|---------------|
| The AI sneaks a write into a "read" | `query` rejects anything that isn't a read statement, and runs reads inside `START TRANSACTION READ ONLY`, so MySQL refuses writes anyway |
| The AI changes data without asking | `mutate` asks you every single time, showing the SQL, params and estimated affected rows. It runs only if you tick **Approve** and accept |
| No answer | Fails closed. Declining, cancelling or a 5-minute timeout means the statement is **not** run |
| A client that can't show the server prompt | Falls back to the client's own tool permission prompt for `mutate` (keep it on **Ask**, not **Always allow**) |
| "Always allow" set on the tool by mistake | The server still asks when the client supports approval prompts. Clients without them rely on the tool prompt only |
| Dropping or altering tables | DDL (`CREATE`, `ALTER`, `DROP`, `TRUNCATE`, `RENAME`, `GRANT`, `REVOKE`) is rejected unless `ALLOW_DDL=true`, and still needs approval when enabled |
| SQL injection tricks | Stacked statements (`SELECT 1; DROP …`), `/*! … */` executable comments and `INTO OUTFILE` are rejected. `SET`, `CALL`, `LOAD` and `LOCK` are not supported |
| "What did it change?" | Every write attempt (executed, declined, blocked or failed) is logged as JSON to stderr and, optionally, to `AUDIT_LOG_FILE` |

Read tools are marked `readOnlyHint: true` and `mutate` is marked `destructiveHint: true`, so clients can show the right level of caution. You can safely set the read tools to **Always allow**, and investigation will flow without prompts.

### What an approval looks like

```
The AI wants to run an UPDATE statement:

UPDATE orders SET status = ? WHERE id = ?

Params: ["shipped", 1001]

Estimated rows affected: ~1

[ ] Approve this database change        [Accept] [Decline]
```

### Recommended setup

1. **Use a dedicated MySQL user, not `root`.** The database's own permissions are the strongest guard.
   ```sql
   -- Investigation only
   CREATE USER 'claude_reader'@'localhost' IDENTIFIED BY '...';
   GRANT SELECT ON your_database.* TO 'claude_reader'@'localhost';

   -- Or allow approved writes on specific tables
   GRANT SELECT, INSERT, UPDATE, DELETE ON your_database.orders TO 'claude_writer'@'localhost';
   ```
2. **Turn on `READ_ONLY=true`** when you only want investigation.
3. **Point it at local or staging databases.** Don't connect it to production.
4. **Set `AUDIT_LOG_FILE`** if you want a record of every change.

---

## Configuration

| Variable | Default | Description |
|----------|---------|-------------|
| `DB_HOST` | `localhost` | MySQL host |
| `DB_PORT` | `3306` | MySQL port |
| `DB_USER` | `root` | MySQL user |
| `DB_PASSWORD` | _(empty)_ | MySQL password |
| `DB_NAME` | `mydb` | Database name |
| `DB_POOL_LIMIT` | `10` | Connection pool size |
| `READ_ONLY` | `false` | `true` removes the `mutate` tool |
| `ALLOW_DDL` | `false` | `true` allows schema changes through `mutate` (still approved) |
| `AUDIT_LOG_FILE` | _(unset)_ | File to append one JSON line per write attempt |

## Releasing

Releases are built automatically by [`.github/workflows/release.yml`](.github/workflows/release.yml):

```bash
git tag v1.0.3 && git push origin v1.0.3
```

The workflow builds the server, sets the version from the tag, publishes [`mysql-mate`](https://www.npmjs.com/package/mysql-mate) to npm (with provenance), packs the `.mcpb` bundle and attaches it to a GitHub Release. You can also run it by hand from the **Actions** tab. A version that's already on npm is skipped, so re-running a release is safe.

**npm authentication**, one of:

- **Trusted publishing (recommended):** on npmjs.com, open the package's **Settings → Trusted publishing**, add GitHub Actions with repository `iamdylanngo/mysql-mate` and workflow `release.yml`. No secret needed.
- **Token:** create a granular access token with publish rights and save it as the `NPM_TOKEN` repository secret.

Trusted publishing can only be set up once the package exists, so do the very first publish by hand:

```bash
npm login
npm publish --access public
```

## Tech stack

- [MCP SDK](https://github.com/modelcontextprotocol/typescript-sdk): MCP server framework
- [mysql2](https://github.com/sidorares/node-mysql2): MySQL client with promise support
- [Zod](https://zod.dev/): input schema validation
- TypeScript

## License

[MIT](./LICENSE)

TDQS

C2.9/5.0

Scored across 5 tools

Disambiguation5/5

Each tool has a distinct purpose: listing tables, describing a table, running SELECT queries, running INSERT/UPDATE/DELETE, and fetching MySQL version. There is no overlap.

Naming Consistency3/5

Tool names mix verb_noun patterns (list_tables, describe_table) with single-verb names (mutate, query) and a verbose name (select_mysql_version). The inconsistency could confuse an agent about naming conventions.

Tool Count5/5

With 5 tools covering listing, describing, querying, mutating, and version checking, the tool count is well-scoped for a MySQL server providing basic database interaction.

Completeness3/5

The tools cover essential read/write operations and schema inspection, but lack DDL tools like create or drop table. The mutate tool is limited to DML, so agents cannot perform schema changes.

Maintenance

ActivityMaintained
ResponsivenessLimited data