Skip to main content

MySQL MCP Server for AI database operations

Project description

MCP MySQL Tool

A powerful and comprehensive MySQL Model Context Protocol (MCP) server that allows AI assistants to interact with MySQL databases effortlessly. Features 12 specialized tools for different database operations with full cross-database support.

🚀 Features

Core Operations

  • mysql_query - Execute any SQL query with parameters
  • mysql_select - Smart SELECT with WHERE, LIMIT, ORDER BY, and database support
  • mysql_insert - Insert data with key-value pairs across databases
  • mysql_update - Update with required WHERE clause (safety) across databases
  • mysql_delete - Delete with required WHERE clause (safety) across databases

Database Exploration

  • mysql_show_databases - List all available databases
  • mysql_show_tables - List all tables in specified or current database
  • mysql_describe_table - Show table structure and columns from any database
  • mysql_table_info - Comprehensive table analysis (structure, indexes, constraints) from any database
  • mysql_count_rows - Count rows with optional WHERE conditions from any database

Advanced Features

  • mysql_custom_connection - Execute queries with custom host/user/password (password optional)
  • mysql_test_connection - Test connection with environment or custom parameters
  • Cross-database operations - All tools support database parameter for multi-database workflows
  • Safe parameterized queries to prevent SQL injection
  • Environment-based configuration
  • Comprehensive error handling
  • JSON responses optimized for AI parsing

📦 Installation

pip install mcp-mysql-tool

⚙️ Configuration

Environment Variables

Set these environment variables for default connection:

export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_USER=your_username
export MYSQL_PASSWORD=your_password
export MYSQL_DATABASE=your_database  # Optional

MCP Client Configuration

Add to your MCP client configuration:

{
  "mcpServers": {
    "mysql": {
      "command": "mcp-mysql-tool",
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_USER": "root",
        "MYSQL_PASSWORD": "password",
        "MYSQL_DATABASE": "mydb"
      }
    }
  }
}

🛠️ Usage Examples

Basic Operations

# List all databases
mysql_show_databases()

# Smart SELECT with conditions
mysql_select({
  "table": "users",
  "columns": ["id", "name", "email"],
  "where": "active = 1",
  "limit": 10,
  "order_by": "created_at DESC"
})

# SELECT from specific database
mysql_select({
  "table": "posts",
  "database": "wordpress",
  "where": "post_status = 'publish'",
  "limit": 5
})

# Safe INSERT
mysql_insert({
  "table": "users",
  "data": {
    "name": "John Doe",
    "email": "john@example.com",
    "active": 1
  }
})

# INSERT into specific database
mysql_insert({
  "table": "products",
  "database": "ecommerce",
  "data": {
    "name": "Widget",
    "price": 29.99,
    "category": "tools"
  }
})

# Safe UPDATE (WHERE required)
mysql_update({
  "table": "users",
  "data": {"last_login": "2024-01-01"},
  "where": "id = 123"
})

# UPDATE across databases
mysql_update({
  "table": "orders",
  "database": "shop",
  "data": {"status": "shipped"},
  "where": "order_id = 456"
})

Database Exploration

# List tables in current database
mysql_show_tables()

# List tables in specific database
mysql_show_tables({"database": "wordpress"})

# Describe table structure
mysql_describe_table({"table_name": "users"})

# Describe table from specific database
mysql_describe_table({
  "table_name": "wp_posts",
  "database": "wordpress"
})

# Get comprehensive table info
mysql_table_info({"table_name": "users"})

# Get table info from specific database
mysql_table_info({
  "table_name": "wp_users",
  "database": "wordpress"
})

Custom Connection

# Connect to different server (password now optional)
mysql_custom_connection({
  "host": "remote-server.com",
  "user": "remote_user",
  "query": "SELECT COUNT(*) FROM products"
})

Row Counting

# Count rows with conditions
mysql_count_rows({
  "table": "orders",
  "where": "status = 'completed' AND created_at > '2024-01-01'"
})

# Count from specific database
mysql_count_rows({
  "table": "posts",
  "database": "blog",
  "where": "published = 1"
})

🔒 Safety Features

  • Required WHERE clauses for UPDATE and DELETE operations
  • Parameterized queries to prevent SQL injection
  • Connection isolation for custom connections
  • Comprehensive error handling with detailed error codes
  • Input validation for all parameters

📋 Available Tools

Tool Description Database Support Safety
mysql_query Execute any SQL query ✅ Full ⚠️ Raw SQL
mysql_select Smart SELECT builder with WHERE/LIMIT/ORDER BY ✅ Parameter ✅ Safe
mysql_insert Insert with key-value pairs ✅ Parameter ✅ Safe
mysql_update Update with required WHERE ✅ Parameter 🔒 WHERE required
mysql_delete Delete with required WHERE ✅ Parameter 🔒 WHERE required
mysql_custom_connection Custom host/user/password (password optional) ✅ Full ⚠️ Raw SQL
mysql_show_databases List databases ✅ N/A ✅ Safe
mysql_show_tables List tables in database ✅ Parameter ✅ Safe
mysql_describe_table Table structure and columns ✅ Parameter ✅ Safe
mysql_table_info Comprehensive table analysis ✅ Parameter ✅ Safe
mysql_count_rows Count with optional WHERE ✅ Parameter ✅ Safe
mysql_test_connection Test connection (env/custom) ✅ Full ✅ Safe

🎯 Perfect for AI

This MCP server is specifically designed to be AI-friendly:

  • Structured responses in JSON format
  • Clear error messages with error codes
  • Flexible parameters for different use cases
  • Cross-database operations with database parameter
  • Safety guardrails to prevent dangerous operations
  • Comprehensive toolset for all database needs

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_tool-0.2.3.tar.gz (12.8 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_tool-0.2.3-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file mcp_mysql_tool-0.2.3.tar.gz.

File metadata

  • Download URL: mcp_mysql_tool-0.2.3.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.6

File hashes

Hashes for mcp_mysql_tool-0.2.3.tar.gz
Algorithm Hash digest
SHA256 b61ba89262547dc3c409237a053d4f718db2e5bed71e6cb0d051e91dc0af021e
MD5 e2eaaa87904511eaa2a51a6b3c52f738
BLAKE2b-256 f0b51de1ea725dfa8ac75bb1de3864aece12f1354dc6c644b6f76c32a74aa11c

See more details on using hashes here.

File details

Details for the file mcp_mysql_tool-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: mcp_mysql_tool-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.6

File hashes

Hashes for mcp_mysql_tool-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 87809cb77e76208b6187019183172a188cfd413b422f66d3794231a98e2e8f25
MD5 5c78ea480c372b49d3b6e1283d37a33c
BLAKE2b-256 cbb4abe02f2d342ddc69990f457ee94bc0b1f5366e6b6d3bcc55bf4f3542f9dc

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