MySQL Database MCP server

Integrates with MySQL databases to enable secure query execution, schema management, and CRUD operations for structured data access and manipulation
Back to servers
Setup instructions
Provider
enemyrr
Release date
Feb 09, 2025
Language
TypeScript
Stats
27 stars

This MCP MySQL Server provides a standardized interface for AI models to interact with MySQL databases, enabling database operations through the Model Context Protocol. It's designed to work seamlessly with tools like Claude Desktop and Cursor IDE.

Installation

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

If you prefer a manual installation:

  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:
    • Open Command Palette (Cmd/Ctrl + Shift + P)
    • Search for "MCP: Add Server"
    • Fill in these fields:
      • Name: mysql
      • Type: command
      • Command: node /absolute/path/to/mcp-mysql-server/build/index.js

Note: Replace /absolute/path/to/ with the actual path where you cloned the project.

Database Configuration

You can configure your database connection in three different ways:

1. Database URL in .env (Recommended)

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

2. Individual Parameters in .env

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 Tools

Connecting to a Database

Before executing any queries, you need to connect to your database:

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

Querying Data

To retrieve data from the database:

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

Modifying Data

To insert, update, or delete data:

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

Database Schema Operations

Listing Tables

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

Describing a Table Structure

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

Creating a New Table

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 a Table

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

Security Notes

The MCP MySQL server implements several security features:

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

When using the server, always ensure your database credentials are stored securely and not committed to version control.

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "mysql" '{"command":"node","args":["/absolute/path/to/mcp-mysql-server/build/index.js"]}'

See the official Claude Code MCP documentation for more details.

For 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 > Tools & Integrations and click "New MCP Server".

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

{
    "mcpServers": {
        "mysql": {
            "command": "node",
            "args": [
                "/absolute/path/to/mcp-mysql-server/build/index.js"
            ]
        }
    }
}

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 explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "mysql": {
            "command": "node",
            "args": [
                "/absolute/path/to/mcp-mysql-server/build/index.js"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

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