yuki777
服务介绍
MySQL MCP Server
The MySQL Model Context Protocol (MCP) server is a tool that connects to a local MySQL database, enabling large language models (LLMs) to execute SQL queries.
Requirements
- Node.js: 20.0.0 or higher
- MySQL: MySQL or MariaDB server version 5.7 or higher
Features
- Execution of MySQL Queries: Directly execute SQL queries from LLMs
- Database Information Retrieval: Obtain a list of databases, tables, and table structures
- MCP Compliance: Complies with the Model Context Protocol, allowing integration with LLMs
- stdio Communication: Communicates with LLMs using standard input and output, without binding to a port
- Connection Profile Management: Manage multiple connection settings by profile name and switch between them
- Saving Connection Information: Save database connection information locally for reuse
Installation and Usage
Temporary Execution with NPX
bash
npx -y https://github.com/yuki777/mysql-mcp-server --host 127.0.0.1 --port 13306 --user root
Options
| Option | Description | Default Value |
|---|---|---|
-h, --host <host> |
MySQL host | localhost |
-p, --port <port> |
MySQL port | 13306 |
-u, --user <user> |
MySQL user | root |
--password <password> |
MySQL password | (empty string) |
-d, --database <database> |
Default database | (optional) |
-c, --config <path> |
Configuration file path | (optional) |
--auto-connect |
Automatically connect to the database when the server starts | false |
--server-port <port> |
MCP server port (not used in stdio mode) | 3000 |
--server-host <host> |
MCP server host (not used in stdio mode) | localhost |
--query-timeout <ms> |
Query timeout (milliseconds) | 30000 |
--max-results <count> |
Maximum number of result rows | 1000 |
--debug |
Debug mode | false |
Saving and Reusing Connection Information
MySQL MCP Server saves the information of successfully connected databases as named profiles locally. This allows you to reuse the connection information by specifying the profile name on the next startup. The saved connection information is stored in the .mysql-mcp-connections.json file in the user's home directory.
Each connection profile includes:
- Profile name
- Hostname
- Port number
- Username
- Password
- Database name (if set)
You can manage multiple database connections by profile name and easily switch between them.
Using Configuration Files
You can also set up connection information using a configuration file (in JSON format):
json
{
"server": {
"port": 3000,
"host": "localhost"
},
"mysql": {
"host": "localhost",
"port": 13306,
"user": "root",
"password": "yourpassword",
"database": "mydb"
},
"debug": false,
"queryTimeout": 30000,
"maxResultSize": 1000
}
To use a configuration file:
bash
npx -y https://github.com/yuki777/mysql-mcp-server -c ./mysql-mcp-config.json
Communication Method
MySQL MCP Server operates in "stdio" mode, which complies with the Model Context Protocol (MCP). This allows it to communicate through standard input and output without binding to a specific port. The benefits include:
- Avoiding Port Conflicts: Since no specific port is used, there are no port conflict issues.
- Enhanced Security: Not using network communication reduces the risk of network-level attacks.
- Simplified Inter-Process Communication: Simplifies communication with LLMs.
Notes- In stdio mode, messages are exchanged in JSON format.
- One JSON message must be sent per line.
- Error information and connection logs are output to standard error (stderr).
Provided MCP Tools
Database Connection Management
| Tool Name | Description | Required Parameters |
|---|---|---|
| connect_database | Connects to the database | host, port, user |
| connect_by_profile | Connects using a saved profile name | profileName |
| disconnect_database | Disconnects from the current database | None |
| get_connection_status | Retrieves the status of the database connection | None |
Connection Profile Management
| Tool Name | Description | Required Parameters |
|---|---|---|
| list_profiles | Retrieves a list of saved profiles | None |
| get_profile | Retrieves details of a profile | profileName |
| add_profile | Adds a new profile | profileName, host, port, user |
| remove_profile | Removes a profile | profileName |
SQL Query Operations
| Tool Name | Description | Required Parameters |
|---|---|---|
| execute_query | Executes a MySQL query | query: SQL statement |
| get_databases | Retrieves a list of available databases | None |
| get_tables | Retrieves a list of tables within a specified database | database (optional) |
| describe_table | Retrieves the structure of a specified table | table |
Connection Management Features
The MySQL MCP Server allows for the separation of server startup and database connections. This approach offers the following benefits:
- Startup without Connection Information: The server can start even without database connection information.
- Connection to Multiple Databases: After the server starts, it is possible to switch connections to different databases.
- Simple Installation: Can be executed with just
npx -y https://github.com/yuki777/mysql-mcp-server.
How to Use Connection Management
-
Start the server without automatic connection:
bash
npx -y https://github.com/yuki777/mysql-mcp-server -
Connect to the database using the connection tool (specify and save the profile name):
json
{
"type": "tool_call",
"request_id": "req_1",
"tool": "connect_database",
"arguments": {
"host": "localhost",
"port": 3306,
"user": "root",
"password": "your_password",
"database": "your_db",
"profileName": "my-db"
}
} -
Retrieve the list of profiles:
json
{
"type": "tool_call",
"request_id": "req_2",
"tool": "list_profiles",
"arguments": {}
} -
Connect by profile name:
json
{
"type": "tool_call",
"request_id": "req_3",
"tool": "connect_by_profile",
"arguments": {
"profileName": "my-db"
}
} -
Add a new profile (without connecting):
json
{
"type": "tool_call",
"request_id": "req_4",
"tool": "add_profile",
"arguments": {
"profileName": "production-db",
"host": "prod.example.com",
"port": 3306,
"user": "prod_user",
"password": "prod_password",
"database": "production"
}
} -
Check the connection status:
json
{
"type": "tool_call",
"request_id": "req_5",
"tool": "get_connection_status",
"arguments": {}
} -
Disconnect from the database:
json
{
"type": "tool_call",
"request_id": "req_6",
"tool": "disconnect_database",
"arguments": {}
}
Test ScriptThe repository includes a test script named test-connection-management.js. You can use this script to test the connection management features:
bash
node test-connection-management.js
Information for Developers
Setting Up the Development Environment
bash
Clone the repository
git clone [repository-url]
cd mysql-mcp-server
Install dependencies
npm install
Run in development mode
npm run dev
Building
bash
npm run build
License
ISC
Contributions
We welcome bug reports, feature requests, and pull requests.