MySQL Database MCP server

Enables direct MySQL database operations including query execution, schema management, and table information retrieval with built-in security features to prevent SQL injection attacks.
Back to servers
Provider
enemyrr
Release date
Mar 18, 2025
Language
TypeScript

The MCP MySQL Server allows AI models to interact with MySQL databases through a standardized interface, enabling you to execute queries, manage database schemas, and perform other database operations directly through your AI assistant.

Installation Options

Installing via Smithery

The easiest way to install the MySQL Database Server is automatically via Smithery:

npx -y @smithery/cli install @enemyrr/mcp-mysql-server --client claude

Installing Manually

To install the server manually:

  1. Clone and build the project:
git clone https://github.com/enemyrr/mcp-mysql-server.git
cd mcp-mysql-server
npm install
npm run build
  1. Add the server in Cursor IDE settings:
    • Open Command Palette (Cmd/Ctrl + Shift + P)
    • Search for "MCP: Add Server"
    • Fill in the fields:
      • Name: mysql
      • Type: command
      • Command: node /absolute/path/to/mcp-mysql-server/build/index.js

Note: Make sure to replace /absolute/path/to/ with the actual path where you cloned the project.

Database Configuration

You can configure your database connection using one of these three methods:

1. Database URL in .env file (Recommended)

DATABASE_URL=mysql://user:password@host:3306/database

2. Individual Parameters in .env file

DB_HOST=localhost
DB_USER=your_user
DB_PASSWORD=your_password
DB_DATABASE=your_database

3. Direct Connection via Tool

use_mcp_tool({
  server_name: "mysql",
  tool_name: "connect_db",
  arguments: {
    url: "mysql://user:password@host:3306/database"
    // OR
    workspace: "/path/to/your/project" // Will use project's .env
    // OR
    host: "localhost",
    user: "your_user",
    password: "your_password",
    database: "your_database"
  }
});

Using the MySQL Tools

Connecting to a Database

Before using any other tool, you need to establish a connection:

use_mcp_tool({
  server_name: "mysql",
  tool_name: "connect_db",
  arguments: {
    url: "mysql://user:password@host:3306/database"
  }
});

Querying Data (SELECT)

For retrieving data from the database:

use_mcp_tool({
  server_name: "mysql",
  tool_name: "query",
  arguments: {
    sql: "SELECT * FROM users WHERE id = ?",
    params: [1]
  }
});

Executing Modifications (INSERT, UPDATE, DELETE)

For modifying data in the database:

use_mcp_tool({
  server_name: "mysql",
  tool_name: "execute",
  arguments: {
    sql: "INSERT INTO users (name, email) VALUES (?, ?)",
    params: ["John Doe", "[email protected]"]
  }
});

Listing All Tables

To get a list of all tables in your database:

use_mcp_tool({
  server_name: "mysql",
  tool_name: "list_tables"
});

Describing a Table Structure

To get detailed information about a specific table:

use_mcp_tool({
  server_name: "mysql",
  tool_name: "describe_table",
  arguments: {
    table: "users"
  }
});

Creating a New Table

To create a new table with specified fields and indexes:

use_mcp_tool({
  server_name: "mysql",
  tool_name: "create_table",
  arguments: {
    table: "users",
    fields: [
      {
        name: "id",
        type: "int",
        autoIncrement: true,
        primary: true
      },
      {
        name: "email",
        type: "varchar",
        length: 255,
        nullable: false
      }
    ],
    indexes: [
      {
        name: "email_idx",
        columns: ["email"],
        unique: true
      }
    ]
  }
});

Adding a Column to an Existing Table

To add a new column to an existing table:

use_mcp_tool({
  server_name: "mysql",
  tool_name: "add_column",
  arguments: {
    table: "users",
    field: {
      name: "phone",
      type: "varchar",
      length: 20,
      nullable: true
    }
  }
});

Security and Error Handling

The server includes several built-in security features:

  • Uses prepared statements to prevent SQL injection
  • Supports secure password handling through environment variables
  • Validates queries before execution
  • Automatically closes connections when done

If you encounter errors, the server will return detailed error messages for connection failures, invalid queries or parameters, missing configuration, and more.

How to add this MCP server to Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later