MySQL MCP Server
MySQL MCP サーバー
MySQL データベースと対話するための MCP サーバー。
このサーバーは、読み取り専用クエリ (query) と、最終的にロールバックされる書き込みクエリ (test_execute) の実行をサポートしています。
設定
環境変数
~/.mcp/.envに次の環境変数を追加します。
MYSQL_HOST=host.docker.internal # Hostname to access host services from Docker container
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password注:
host.docker.internal、Docker コンテナからホストマシンのサービスにアクセスするための特別な DNS 名です。ホストマシン上で実行されている MySQL サーバーに接続する場合は、この設定を使用してください。別の MySQL サーバーに接続する場合は、適切なホスト名に変更してください。
mcp.json 構成
{
"mcpServers": {
"mysql": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--add-host=host.docker.internal:host-gateway",
"--env-file",
"/Users/username/.mcp/.env",
"ghcr.io/xiangma9712/mcp/mysql"
]
}
}
}Related MCP server: MCP MySQL Server
使用法
サーバーの起動
docker run -i --rm --add-host=host.docker.internal:host-gateway --env-file ~/.mcp/.env ghcr.io/xiangma9712/mcp/mysql注:OrbStackを使用している場合は、
host.docker.internalが自動的にサポートされるため、--add-hostオプションは省略できます。Docker Desktopも通常はこれを自動的にサポートしますが、信頼性を高めるために--add-hostオプションを追加することをお勧めします。
利用可能なコマンド
1. 読み取り専用クエリを実行する
{
"type": "query",
"payload": {
"sql": "SELECT * FROM your_table"
}
}応答:
{
"success": true,
"data": [
{
"id": 1,
"name": "example"
}
]
}2. クエリ実行のテスト
{
"type": "test_execute",
"payload": {
"sql": "UPDATE your_table SET name = 'updated' WHERE id = 1"
}
}応答:
{
"success": true,
"data": "The UPDATE SQL query can be executed."
}3. リストテーブル
{
"type": "list_tables"
}応答:
{
"success": true,
"data": ["table1", "table2", "table3"]
}4. 表の説明
{
"type": "describe_table",
"payload": {
"table": "your_table"
}
}応答:
{
"success": true,
"data": [
{
"Field": "id",
"Type": "int(11)",
"Null": "NO",
"Key": "PRI",
"Default": null,
"Extra": ""
},
{
"Field": "name",
"Type": "varchar(255)",
"Null": "YES",
"Key": "",
"Default": null,
"Extra": ""
}
]
}実装の詳細
TypeScriptで実装
mysql2 パッケージを使用
Dockerコンテナとして実行
標準入力を通じてJSONコマンドを受け入れる
標準出力を通じてJSONレスポンスを返します
ホスト MySQL に接続するために
host.docker.internalを使用します (OrbStack と Docker Desktop の両方と互換性があります)
セキュリティに関する考慮事項
機密情報の管理に環境変数を使用する
SQLインジェクションの防止は実装者の責任である
実稼働環境での使用には適切なネットワーク構成が必要
ホストマシンのサービスに接続する際に必要な適切なファイアウォール設定
Available Tools
4 toolsdescribe_tableC
Retrieves column information for a table.
Inputs: tableName (required).
list_tablesB
Retrieves a list of tables in the database.
queryB
Executes a read-only SQL query.
Inputs: sql (required).
test_executeB
Checks if an SQL query can be executed and rolls back afterward.
Inputs: sql (required).
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 4 tools
Each tool has a clearly distinct purpose: describe_table for column metadata, list_tables for table enumeration, query for read-only SQL execution, and test_execute for query validation with rollback. There is no overlap in functionality, making tool selection straightforward for an agent.
The naming follows a consistent verb_noun pattern (describe_table, list_tables, query, test_execute), with 'query' as a minor deviation as it lacks a noun suffix. Overall, the pattern is predictable and readable, though not perfectly uniform.
With 4 tools, the count is reasonable for a database server, covering essential operations like listing tables, describing schema, querying, and testing queries. It is slightly lean but well-scoped, lacking only advanced features like write operations or transaction management.
The toolset covers basic read and validation operations but has notable gaps: there are no tools for data manipulation (e.g., insert, update, delete), schema modification (e.g., create_table), or transaction control. This limits the server to read-only and diagnostic tasks, which may cause agent failures for write workflows.
Maintenance
Related MCP Connectors
Work with your database from AI tools: ask, export, approved changes, and scheduled jobs.
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Hosted MySQL MCP server for Claude, ChatGPT and Cursor, with each action switched per account.
Text-to-SQL MCP server: read-only queries on PostgreSQL, MySQL and SQL Server with your real schema
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceEnables secure interaction with MySQL databases, allowing AI assistants to list tables, read data, and execute SQL queries through a controlled interface.-
- AlicenseNot gradedqualityDmaintenanceEnables interaction with MySQL databases through MCP, supporting query execution, table operations (insert, update, delete), and schema inspection for natural language database management.ISC
- AlicenseNot gradedqualityDmaintenanceEnables AI models to interact with MySQL databases through standardized operations including querying, executing commands, listing tables, and describing table structures with secure prepared statement support.MIT
- AlicenseAqualityDmaintenanceEnables querying local MySQL databases and managing Docker containers (listing, logs) through natural language via the Model Context Protocol.455 npmISC