Skip to main content

MCP Server to connect the MySQL database

Project description

MySQL MCP Server

A Model Context Protocol (MCP) server that provides an interface for AI models to interact with MySQL databases through natural language queries.

Overview

This package creates an MCP server that connects to a MySQL database and exposes a tool to execute SQL queries. It allows AI models like Claude to interact with your MySQL database without direct database access.

Prerequisites

  • Python 3.11 or above
  • MySQL database

Installation

You can install the MCP MySQL server using pip:

pip install mcp_mysql_connect

Or using UV:

uv pip install mcp_mysql_connect

Configuration

The server requires the following environment variables:

  • DB_HOST - MySQL server host
  • DB_USER - MySQL username
  • DB_PASSWORD - MySQL password
  • DB_NAME - MySQL database name

You can set these using a .env file or directly in your environment.

Usage Options

1. Using with Cursor

Add the following to your ~/.cursor/mcp.json configuration:

"mysql-mcp-server": {
  "command": "uvx",
  "args": [
    "--from",
    "mcp_mysql_connect",
    "mcp_mysql_connect"
  ],
  "env": {
    "DB_HOST": "your-mysql-host",
    "DB_USER": "your-username",
    "DB_PASSWORD": "your-password",
    "DB_NAME": "your-database"
  }
}

Alternative configuration:

"mysql-mcp-server": {
  "command": "uv",
  "args": [
    "run",
    "--with",
    "mcp_mysql_connect",
    "-m",
    "mcp_mysql_connect"
  ],
  "env": {
    "DB_HOST": "your-mysql-host",
    "DB_USER": "your-username",
    "DB_PASSWORD": "your-password",
    "DB_NAME": "your-database"
  }
}

2. Using with Python Code

from mcp.client import Client
from mcp.tools import Tools

# Connect to the MySQL MCP server
tools = Tools()
tools.add_server("mysql", "mcp_mysql_connect")

# Create a client with the tools
client = Client(tools=tools)

# Example query
response = client.complete(
    messages=[
        {"role": "user", "content": "Query all users from the database"}
    ]
)
print(response.content)

3. Running Manually

Start the server manually:

# Set environment variables first
export DB_HOST=your-mysql-host
export DB_USER=your-username
export DB_PASSWORD=your-password
export DB_NAME=your-database

# Then run the server
python -m mcp_mysql_connect

Available Tools

The server provides a SQL execution tool that can be used to query your MySQL database:

  • read_query(query: str, params: List[str] = None) - Execute a SQL SELECT query against the database

Example Queries

When using with Claude or other AI models, you can ask natural language questions like:

  • "Show me all users who registered in the last week"
  • "What are the top 5 selling products?"
  • "Count how many orders each customer has placed"

The AI model will translate these into SQL queries and use the MCP server to execute them.

License

MIT License

Contributing

Contributions welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_mysql_connect-1.0.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_mysql_connect-1.0.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file mcp_mysql_connect-1.0.2.tar.gz.

File metadata

  • Download URL: mcp_mysql_connect-1.0.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for mcp_mysql_connect-1.0.2.tar.gz
Algorithm Hash digest
SHA256 1615e1c674b53ff344d257a33e746abf4e0743a5431a3abc56eae10c5a7da155
MD5 6002942c413bb164d2e9b2f306bb7557
BLAKE2b-256 2070b35ec5e2ffa1dffb93a635a8ea45486ba4b9fa67db135d1ba9515dbd4172

See more details on using hashes here.

File details

Details for the file mcp_mysql_connect-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_mysql_connect-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e6c992d37489a66675008a1fb7b736d8afed63ba03d8acaf04c36997e2dd62b6
MD5 74969a2ceac559bc19f1d9e977042427
BLAKE2b-256 df01bbb64c4fcc414aeb64f71197ae6058438a750fd3627f4b06a3d8312cdc33

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page