Skip to main content

A Model Context Protocol (MCP) server that enables secure interaction with MySQL databases. This server allows AI assistants to list tables, read data, and execute SQL queries through a controlled interface, making database exploration and analysis safer and more structured.

Project description

MySQL MCP Server Pro Plus

A robust, secure, and feature-rich Model Context Protocol (MCP) server for MySQL databases. This server provides a standardized interface for AI assistants to interact with MySQL databases through tools and resources.

๐Ÿš€ Features

Core Features

  • Secure Database Operations: Input validation, SQL injection protection, and query sanitization
  • Connection Pooling: Efficient connection management with configurable pool settings
  • Type Safety: Full type annotations and Pydantic models for configuration validation
  • Comprehensive Error Handling: Detailed error messages and proper exception handling
  • Async/Await Support: Modern async patterns for better performance
  • Resource Management: Proper cleanup of database connections and cursors
  • Test Data Generation: Comprehensive e-commerce database simulation with 10M+ rows and bad practices for MCP agent testing

Tools Available

  • execute_sql: Execute custom SQL queries with result formatting
  • list_tables: List all tables in the database
  • describe_table: Get detailed table structure information

Resources

  • Table Data: Access table contents as CSV-formatted resources
  • Automatic Discovery: Dynamic table listing and resource creation

๐Ÿ“‹ Requirements

  • Python 3.8+
  • MySQL 5.7+ or MariaDB 10.2+
  • mysql-connector-python

๐Ÿ› ๏ธ Installation

  1. Clone the repository:

    git clone <repository-url>
    cd mysql_mcp_server_pro_plus
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Set up environment variables:

    cp env.example .env
    # Edit .env with your MySQL configuration
    

โš™๏ธ Configuration

Environment Variables

Variable Description Default Required
MYSQL_URL MySQL connection URL (preferred) - No*
MYSQL_HOST MySQL server host localhost No
MYSQL_PORT MySQL server port 3306 No
MYSQL_USER MySQL username - Yes
MYSQL_PASSWORD MySQL password - Yes
MYSQL_DATABASE MySQL database name - Yes
MYSQL_CHARSET Character set utf8mb4 No
MYSQL_COLLATION Collation utf8mb4_unicode_ci No
MYSQL_AUTOCOMMIT Auto-commit mode true No
MYSQL_SQL_MODE SQL mode TRADITIONAL No
MYSQL_CONNECTION_TIMEOUT Connection timeout (seconds) 10 No
MYSQL_POOL_SIZE Connection pool size 5 No
MYSQL_POOL_RESET_SESSION Reset session on return true No

Note: Either MYSQL_URL or the individual MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE variables are required. If MYSQL_URL is provided, it takes precedence over individual variables.

Example Configuration

# .env file

# Option 1: Using MySQL URL (Recommended)
MYSQL_URL=mysql://myuser:mypassword@localhost:3306/mydatabase?charset=utf8mb4&collation=utf8mb4_unicode_ci&sql_mode=TRADITIONAL

# Option 2: Using individual variables
# MYSQL_HOST=localhost
# MYSQL_PORT=3306
# MYSQL_USER=myuser
# MYSQL_PASSWORD=mypassword
# MYSQL_DATABASE=mydatabase
# MYSQL_CHARSET=utf8mb4
# MYSQL_COLLATION=utf8mb4_unicode_ci
# MYSQL_AUTOCOMMIT=true
# MYSQL_SQL_MODE=TRADITIONAL
# MYSQL_CONNECTION_TIMEOUT=10
# MYSQL_POOL_SIZE=5
# MYSQL_POOL_RESET_SESSION=true

๐Ÿ“Š Performance

Optimizations

  • Connection Pooling: Reuses database connections for better performance
  • Async Operations: Non-blocking database operations
  • Efficient Query Execution: Optimized query handling and result processing
  • Memory Management: Proper cleanup prevents memory leaks

Monitoring

  • Comprehensive Logging: Detailed logs for debugging and monitoring
  • Error Tracking: Structured error reporting with context
  • Performance Metrics: Connection and query performance tracking

๐Ÿ”ง Development

Project Structure

mysql_mcp_server_pro_plus/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ mysql_mcp_server_pro_plus/
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ””โ”€โ”€ server.py          # Main server implementation
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ conftest.py           # Test configuration
โ”‚   โ””โ”€โ”€ test_server.py        # Server tests
โ”œโ”€โ”€ scripts/
โ”‚   โ””โ”€โ”€ generate_test_data.py # Test data generator (10M+ rows)
โ”œโ”€โ”€ init-scripts/
โ”‚   โ””โ”€โ”€ 01-init.sql          # Database initialization with bad practices
โ”œโ”€โ”€ docker-compose.yml        # Docker Compose configuration
โ”œโ”€โ”€ Dockerfile               # Docker image definition
โ”œโ”€โ”€ pyproject.toml           # Project configuration
โ”œโ”€โ”€ requirements.txt         # Python dependencies
โ”œโ”€โ”€ README.md               # This file
โ””โ”€โ”€ README-TEST-DATA.md     # Test data generation documentation

Test Data Generation

For comprehensive MCP agent testing, the project includes a sophisticated test data generation system:

  • Complex E-commerce Database: 8 interconnected tables with realistic relationships
  • 10+ Million Rows: Distributed across users, products, orders, reviews, and payments
  • 1 Million Transactions: Mixed SELECT, INSERT, UPDATE, and DELETE operations
  • Intentional Bad Practices: Security vulnerabilities, performance issues, and design flaws for MCP agent detection

See README-TEST-DATA.md for detailed documentation.

Quick Start:

# Start the database
make up

# Generate test data (Docker)
make generate-test-data-docker

# Or generate locally
make generate-test-data

# Verify bad practices
make verify-bad-practices-docker

Code Quality

The project follows strict code quality standards:

  • Type Annotations: Full type hints for better IDE support and error detection
  • Pydantic Models: Data validation and serialization
  • Async/Await: Modern Python async patterns
  • Error Handling: Comprehensive exception handling
  • Documentation: Detailed docstrings and comments

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite
  6. Submit a pull request

๐Ÿ› Troubleshooting

Common Issues

Connection Errors

Error: Missing required database configuration

Solution: Ensure all required environment variables are set. Either provide MYSQL_URL or the individual variables (MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE).

Permission Errors

Error: Access denied for user

Solution: Check MySQL user permissions and ensure the user has access to the specified database.

Character Set Issues

Error: Unknown collation

Solution: Update MYSQL_CHARSET and MYSQL_COLLATION to values supported by your MySQL version.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿค Support

For support and questions:

  1. Check the troubleshooting section
  2. Review the test files for usage examples
  3. Open an issue on GitHub
  4. Check the CHANGELOG.md for recent updates

๐Ÿ”„ Changelog

See CHANGELOG.md for a detailed history of changes and improvements.

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

mysql_mcp_server_pro_plus-0.2.8.tar.gz (50.9 kB view details)

Uploaded Source

Built Distribution

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

mysql_mcp_server_pro_plus-0.2.8-py3-none-any.whl (51.1 kB view details)

Uploaded Python 3

File details

Details for the file mysql_mcp_server_pro_plus-0.2.8.tar.gz.

File metadata

  • Download URL: mysql_mcp_server_pro_plus-0.2.8.tar.gz
  • Upload date:
  • Size: 50.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.12.0 readme-renderer/44.0 requests/2.32.3 requests-toolbelt/1.0.0 urllib3/1.26.20 tqdm/4.67.1 importlib-metadata/6.11.0 keyring/25.6.0 rfc3986/1.5.0 colorama/0.4.6 CPython/3.12.2

File hashes

Hashes for mysql_mcp_server_pro_plus-0.2.8.tar.gz
Algorithm Hash digest
SHA256 a65aa81e21a60f07189f8e322fb1f1f90ba99f605a263edc33aba8f43ac99c41
MD5 e7ea76aade7975275107b363e0571e93
BLAKE2b-256 cc273cee64ed45356bf7d20ecc9068b3a8f3152dddc0dfe01f54a343a97dadee

See more details on using hashes here.

File details

Details for the file mysql_mcp_server_pro_plus-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: mysql_mcp_server_pro_plus-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 51.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.12.0 readme-renderer/44.0 requests/2.32.3 requests-toolbelt/1.0.0 urllib3/1.26.20 tqdm/4.67.1 importlib-metadata/6.11.0 keyring/25.6.0 rfc3986/1.5.0 colorama/0.4.6 CPython/3.12.2

File hashes

Hashes for mysql_mcp_server_pro_plus-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 dfc1ed07e98a38b3842a0e816ba9c5d28f2764ec9ac688b2401f5452c7a8718a
MD5 4d56c7344c797c990e613db7af55f7ba
BLAKE2b-256 6bd027929d196ba8fed2eed7ff3098dd12d1631b36dcf16e95e9c8c5ea210833

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