{"id":16644,"library":"mcp-mysql-server","title":"MCP MySQL Server","description":"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.","status":"active","version":"0.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/enemyrr/mcp-mysql-server","tags":["javascript","mcp","model-context-protocol","mysql","database","claude","anthropic"],"install":[{"cmd":"npm install mcp-mysql-server","lang":"bash","label":"npm"},{"cmd":"yarn add mcp-mysql-server","lang":"bash","label":"yarn"},{"cmd":"pnpm add mcp-mysql-server","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Runtime dependency for connecting to and interacting with MySQL databases.","package":"mysql2","optional":false},{"reason":"Required for loading database connection configurations from .env files, as recommended for setup.","package":"dotenv","optional":false}],"imports":[{"note":"This package is an MCP server; its functionality (tools like `connect_db`) is accessed via an MCP client's `use_mcp_tool` abstraction, not through direct JavaScript `import` or `require` statements from user application code.","wrong":"import { connect_db } from '@enemyrr/mcp-mysql-server'","symbol":"connect_db","correct":"await use_mcp_tool({ server_name: \"mysql\", tool_name: \"connect_db\", arguments: { url: \"mysql://user:pass@host:3306/db\" } })"},{"note":"The `query` tool for executing SELECT statements is exposed by the MCP server and invoked through the MCP client's API, facilitating AI model interaction with the database.","wrong":"const query = require('@enemyrr/mcp-mysql-server').query","symbol":"query","correct":"await use_mcp_tool({ server_name: \"mysql\", tool_name: \"query\", arguments: { sql: \"SELECT * FROM users\", params: [] } })"},{"note":"Similar to other tools, the `execute` tool for DML operations (INSERT, UPDATE, DELETE) is a server capability accessed programmatically via the MCP client interface, not a directly importable function or type.","wrong":"type ExecuteArgs = typeof import('@enemyrr/mcp-mysql-server')['execute']","symbol":"execute","correct":"await use_mcp_tool({ server_name: \"mysql\", tool_name: \"execute\", arguments: { sql: \"INSERT INTO logs (message) VALUES (?) \", params: ['test'] } })"},{"note":"Server-provided tools like `list_tables` are part of the MCP contract. Direct file imports from the server package will fail and are not the intended interaction method.","wrong":"import { list_tables } from '@enemyrr/mcp-mysql-server/tools'","symbol":"list_tables","correct":"await use_mcp_tool({ server_name: \"mysql\", tool_name: \"list_tables\" })"}],"quickstart":{"code":"// This code assumes you are running within an environment that provides `use_mcp_tool`,\n// such as Cursor IDE or a compatible AI agent framework.\n// Ensure your .env file in the workspace root has DATABASE_URL=mysql://user:password@host:3306/database\n// or individual DB_HOST, DB_USER, etc.\n\nasync function runDbOperations() {\n  console.log(\"Attempting to connect to MySQL...\");\n  // 1. Connect to the database using environment variables from the workspace\n  const connectResult = await use_mcp_tool({\n    server_name: \"mysql\",\n    tool_name: \"connect_db\",\n    arguments: {\n      workspace: process.env.MCP_WORKSPACE_PATH ?? \"./\" // Uses .env from current directory\n    }\n  });\n\n  if (connectResult.status === 'success') {\n    console.log(\"Successfully connected to MySQL database.\");\n\n    // 2. Execute a simple SELECT query\n    const selectQueryResult = await use_mcp_tool({\n      server_name: \"mysql\",\n      tool_name: \"query\",\n      arguments: {\n        sql: \"SELECT 1 + 1 AS solution, CURRENT_USER() AS user;\",\n        params: []\n      }\n    });\n\n    if (selectQueryResult.status === 'success') {\n      console.log(\"Select Query result:\", selectQueryResult.data);\n    } else {\n      console.error(\"Select Query failed:\", selectQueryResult.error);\n    }\n\n    // 3. Example: Execute an INSERT statement (requires a 'test_table' or similar)\n    //    Ensure a table exists, e.g., CREATE TABLE test_table (id INT AUTO_INCREMENT PRIMARY KEY, message VARCHAR(255));\n    const insertQueryResult = await use_mcp_tool({\n      server_name: \"mysql\",\n      tool_name: \"execute\",\n      arguments: {\n        sql: \"INSERT INTO test_table (message) VALUES (?)\",\n        params: [\"Hello from MCP MySQL Server!\"]\n      }\n    });\n\n    if (insertQueryResult.status === 'success') {\n      console.log(\"Insert Query successful. Rows affected:\", insertQueryResult.data.rowsAffected);\n    } else {\n      console.error(\"Insert Query failed:\", insertQueryResult.error);\n    }\n\n    // 4. List all tables in the connected database\n    const listTablesResult = await use_mcp_tool({\n      server_name: \"mysql\",\n      tool_name: \"list_tables\"\n    });\n\n    if (listTablesResult.status === 'success') {\n      console.log(\"Tables in database:\", listTablesResult.data);\n    } else {\n      console.error(\"List tables failed:\", listTablesResult.error);\n    }\n\n  } else {\n    console.error(\"Failed to connect to MySQL database:\", connectResult.error);\n  }\n}\n\n// In a real AI agent context, this function would be called implicitly or explicitly by the agent.\nrunDbOperations();","lang":"typescript","description":"Demonstrates connecting to a MySQL database using environment variables, executing a simple SELECT query, performing an INSERT statement, and listing database tables via the `use_mcp_tool` abstraction."},"warnings":[{"fix":"Always consult the latest README and changelog for the installed version. Pin dependency versions and test thoroughly before upgrading in production environments.","message":"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.","severity":"breaking","affected_versions":">=0.2.0"},{"fix":"Verify `DATABASE_URL` or individual `DB_HOST`, `DB_USER`, `DB_PASSWORD`, `DB_DATABASE` variables in your `.env` file or the arguments passed to `connect_db`. Ensure the MySQL server is running and accessible from where `mcp-mysql-server` is hosted.","message":"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.","severity":"gotcha","affected_versions":">=0.2.0"},{"fix":"Always use the `params` array with placeholders (`?`) in your `sql` strings for `query` and `execute` tools when incorporating user-supplied or dynamic values. Never concatenate values directly into the `sql` string.","message":"While the provided examples use prepared statements (`sql` with `params`), developers manually constructing SQL strings without parameterization can introduce SQL injection vulnerabilities.","severity":"gotcha","affected_versions":">=0.2.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure 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.","cause":"The MySQL server is not running or is inaccessible from the host running the MCP server, or the specified host/port is incorrect.","error":"Error: connect ECONNREFUSED"},{"fix":"Double-check the `DB_USER` and `DB_PASSWORD` variables in your `.env` file or `connect_db` arguments against your MySQL user credentials.","cause":"Incorrect username or password provided for the MySQL database connection.","error":"Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host' (using password: YES/NO)"},{"fix":"Verify 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.","cause":"The specified database name in the connection string or parameters does not exist on the MySQL server.","error":"Error: ER_BAD_DB_ERROR: Unknown database 'your_database'"},{"fix":"Follow 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.","cause":"The `mcp-mysql-server` has not been correctly registered or configured within your MCP client environment (e.g., Cursor IDE).","error":"MCP server 'mysql' not found (when calling `use_mcp_tool`)"}],"ecosystem":"npm"}