MCP MySQL Server
The @enemyrr/mcp-mysql-server package provides a Model Context Protocol (MCP) server designed to enable AI models, such as those integrated into Cursor IDE or Claude Desktop, to interact with MySQL databases. It offers a standardized interface for AI agents to perform common database operations like connecting, querying, executing DML statements, and managing schema. Currently at version 0.2.0, the package is in early development, implying potentially frequent updates and API changes before a stable 1.0 release. Its key differentiator is simplifying MySQL integration for AI agents through the MCP abstraction, allowing models to operate on databases without needing to handle direct driver interactions or complex connection pooling. Configuration is flexible, supporting database URLs, individual credentials via `.env` files, or direct arguments to the `connect_db` tool.
Common errors
-
Error: connect ECONNREFUSED
cause The MySQL server is not running or is inaccessible from the host running the MCP server, or the specified host/port is incorrect.fixEnsure the MySQL database server is active and reachable. Verify the `DB_HOST` and `DB_PORT` (default 3306) in your `.env` configuration or `connect_db` arguments. -
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host' (using password: YES/NO)
cause Incorrect username or password provided for the MySQL database connection.fixDouble-check the `DB_USER` and `DB_PASSWORD` variables in your `.env` file or `connect_db` arguments against your MySQL user credentials. -
Error: ER_BAD_DB_ERROR: Unknown database 'your_database'
cause The specified database name in the connection string or parameters does not exist on the MySQL server.fixVerify that the `DB_DATABASE` variable in your `.env` file or the `database` argument in `connect_db` matches an existing database on your MySQL server. Create the database if it's missing. -
MCP server 'mysql' not found (when calling `use_mcp_tool`)
cause The `mcp-mysql-server` has not been correctly registered or configured within your MCP client environment (e.g., Cursor IDE).fixFollow the 'Installation & Setup for Cursor IDE' steps in the README. Ensure the `Name` field for the server is exactly `mysql` and the `Command` path points to the correct `index.js` build file of the cloned project.
Warnings
- breaking As a pre-1.0 release (v0.2.0), the API and exposed tool definitions are subject to change without notice. Future versions may introduce breaking changes requiring updates to MCP client integrations.
- gotcha Incorrect or incomplete database configuration (via .env or direct arguments) will lead to connection failures. The server may start but tools will fail to execute with database errors.
- gotcha While the provided examples use prepared statements (`sql` with `params`), developers manually constructing SQL strings without parameterization can introduce SQL injection vulnerabilities.
Install
-
npm install mcp-mysql-server -
yarn add mcp-mysql-server -
pnpm add mcp-mysql-server
Imports
- connect_db
import { connect_db } from '@enemyrr/mcp-mysql-server'await use_mcp_tool({ server_name: "mysql", tool_name: "connect_db", arguments: { url: "mysql://user:pass@host:3306/db" } }) - query
const query = require('@enemyrr/mcp-mysql-server').queryawait use_mcp_tool({ server_name: "mysql", tool_name: "query", arguments: { sql: "SELECT * FROM users", params: [] } }) - execute
type ExecuteArgs = typeof import('@enemyrr/mcp-mysql-server')['execute']await use_mcp_tool({ server_name: "mysql", tool_name: "execute", arguments: { sql: "INSERT INTO logs (message) VALUES (?) ", params: ['test'] } }) - list_tables
import { list_tables } from '@enemyrr/mcp-mysql-server/tools'await use_mcp_tool({ server_name: "mysql", tool_name: "list_tables" })
Quickstart
// This code assumes you are running within an environment that provides `use_mcp_tool`,
// such as Cursor IDE or a compatible AI agent framework.
// Ensure your .env file in the workspace root has DATABASE_URL=mysql://user:password@host:3306/database
// or individual DB_HOST, DB_USER, etc.
async function runDbOperations() {
console.log("Attempting to connect to MySQL...");
// 1. Connect to the database using environment variables from the workspace
const connectResult = await use_mcp_tool({
server_name: "mysql",
tool_name: "connect_db",
arguments: {
workspace: process.env.MCP_WORKSPACE_PATH ?? "./" // Uses .env from current directory
}
});
if (connectResult.status === 'success') {
console.log("Successfully connected to MySQL database.");
// 2. Execute a simple SELECT query
const selectQueryResult = await use_mcp_tool({
server_name: "mysql",
tool_name: "query",
arguments: {
sql: "SELECT 1 + 1 AS solution, CURRENT_USER() AS user;",
params: []
}
});
if (selectQueryResult.status === 'success') {
console.log("Select Query result:", selectQueryResult.data);
} else {
console.error("Select Query failed:", selectQueryResult.error);
}
// 3. Example: Execute an INSERT statement (requires a 'test_table' or similar)
// Ensure a table exists, e.g., CREATE TABLE test_table (id INT AUTO_INCREMENT PRIMARY KEY, message VARCHAR(255));
const insertQueryResult = await use_mcp_tool({
server_name: "mysql",
tool_name: "execute",
arguments: {
sql: "INSERT INTO test_table (message) VALUES (?)",
params: ["Hello from MCP MySQL Server!"]
}
});
if (insertQueryResult.status === 'success') {
console.log("Insert Query successful. Rows affected:", insertQueryResult.data.rowsAffected);
} else {
console.error("Insert Query failed:", insertQueryResult.error);
}
// 4. List all tables in the connected database
const listTablesResult = await use_mcp_tool({
server_name: "mysql",
tool_name: "list_tables"
});
if (listTablesResult.status === 'success') {
console.log("Tables in database:", listTablesResult.data);
} else {
console.error("List tables failed:", listTablesResult.error);
}
} else {
console.error("Failed to connect to MySQL database:", connectResult.error);
}
}
// In a real AI agent context, this function would be called implicitly or explicitly by the agent.
runDbOperations();