Skip to main content

MCP Server for MySQL database operations

Project description

mysql-mcp-server

ํ•œ๊ตญ์–ด README.md

0. Execution

Running with Docker

Change the database connection information as needed.

docker run -d --name mcp-mysql \
  -e MYSQL_HOST=localhost \
  -e MYSQL_PORT=3306 \
  -e MYSQL_USER=root \
  -e MYSQL_PASSWORD=mcpTest1234!!! \
  -e MYSQL_DATABASE=mcp_test \
  -e MCP_PORT=8081 \
  -p 3306:3306 mineru/mcp-mysql:1.0.0

Running with Docker Compose

This will proceed with a pre-configured setup.

docker-compose up -d

Running directly with Python

pip install -r requirements.txt
python mysql_mcp_server/main.py run

Cursor Configuration

MCP functionality is available from Cursor version 0.46 and above.

Additionally, the MCP feature is only accessible to Cursor Pro account users.

Cursor Setting

Tool Addition Tips

  • Adding a Tool
    • execute functions implement the actual logic execution (Service Layer).
    • The @tool decorator helps register the tool with MCP (Controller Layer).
  • Explanation
    • Each file under mysql_mcp_server/executors represents a single tool.
    • If a new tool is added, it must be imported in mysql_mcp_server/executors/__init__.py and included in the __all__ array.
    • This ensures the module is automatically registered in the TOOLS_DEFINITION variable.
flowchart LR;
    A[AI Model] -->|Request tool list| B[MCP Server]
    B -->|Return available tools| A

    A -->|Request specific tool execution| B
    B -->|Call the corresponding executor| C[Executors]
    
    subgraph Executors
        C1[execute_create_table] -->|Create table| D
        C2[execute_desc_table] -->|View table schema| D
        C3[execute_explain] -->|Query execution plan| D
        C4[execute_insert_query] -->|Execute INSERT query| D
        C5[execute_insight_starter] -->|Checking the schema for building reports| D
        C6[execute_invoke_viz_pro] -->|Visualization chart recommendations| D
        C7[execute_select_query] -->|Execute SELECT query| D
        C8[execute_show_tables] -->|Retrieve table list| D
    end

    D[DatabaseManager] -->|Connect to MySQL| E[MySQL 8.0]

    E -->|Return results| D
    D -->|Send results| C
    C -->|Return results| B
    B -->|Return execution results| A

๐Ÿšง Development Roadmap ๐Ÿšง

  • โš™๏ธ Parameter Options

    • ๐Ÿ”ง Enable/Disable Switch for Each Tool: Provide a function to reduce Input Context costs ๐Ÿ’ฐ
    • ๐Ÿ”’ Query Security Level Setting: Offer optional control over functions that could damage asset value, such as DROP, DELETE, UPDATE ๐Ÿšซ
  • โœจ Features

    • ๐Ÿ“Š Data Analysis Report Generation: Provide a report generation function optimized for the model to appropriately select various charts based on user requests ๐Ÿ“ˆ
      • ๐Ÿ“ Reporting capabilities for prescribed forms
      • ๐Ÿ–Œ๏ธ Diversify report templates
    • ๐Ÿ—„๏ธ Extended Text2SQL Support
    • ๐ŸŒ SSH Connection Support: Enable secure remote access via SSH for advanced operations ๐Ÿ”‘
    • ๐Ÿ“ฅ File Extraction Function
      • ๐Ÿ“„ CSV
      • ๐Ÿ“‘ JSON
      • ๐Ÿ“‰ Excel

1. Overview

MCP MySQL Server is a server application for MySQL database operations based on MCP (Model Context Protocol). This server provides tools that allow AI models to interact with the MySQL database.

2. System Configuration

2.1 Key Components

  • MCP Server: A FastMCP server that communicates with AI models
  • MySQL Database: Manages and stores data
  • Tools: Executors that perform database operations

2.2 Tech Stack

  • Language: Python
  • Database: MySQL 8.0
  • Key Libraries:
    • mcp: Implements Model Context Protocol for AI communication
    • PyMySQL: Connects to MySQL and executes queries
    • pandas: Processes and analyzes data
    • python-dotenv: Manages environment variables
    • fire: Implements command-line interfaces

2.3 Deployment Environment

  • Containerized deployment via Docker and Docker Compose
  • Ports: 8081 (MCP Server), 3306 (MySQL)

3. Directory Structure

MCPBoilerPlate/
โ”œโ”€โ”€ mysql_mcp_server/           # Main application directory
โ”‚   โ”œโ”€โ”€ executors/              # Database operation executors
โ”‚   โ”‚   โ”œโ”€โ”€ create_table.py     # Tool for creating tables
โ”‚   โ”‚   โ”œโ”€โ”€ desc_table.py       # Tool for viewing table schema
โ”‚   โ”‚   โ”œโ”€โ”€ explain.py          # Tool for query execution plans
โ”‚   โ”‚   โ”œโ”€โ”€ insert_query.py     # Tool for INSERT query execution
โ”‚   โ”‚   โ”œโ”€โ”€ insight_starter.py  # Schema verification tools for write reports
โ”‚   โ”‚   โ”œโ”€โ”€ invoke_viz_pro.py   # Tool for Visualization chart recommendation
โ”‚   โ”‚   โ”œโ”€โ”€ select_query.py     # Tool for SELECT query execution
โ”‚   โ”‚   โ””โ”€โ”€ show_tables.py      # Tool for retrieving table lists
โ”‚   โ”œโ”€โ”€ helper/                 # Utility modules
โ”‚   โ”‚   โ”œโ”€โ”€ db_conn_helper.py   # Manages database connections
โ”‚   โ”‚   โ”œโ”€โ”€ logger_helper.py    # Logging utilities
โ”‚   โ”‚   โ””โ”€โ”€ tool_decorator.py   # Tool decorator
โ”‚   โ””โ”€โ”€ main.py                 # Application entry point
โ”œโ”€โ”€ docker-compose.yml          # Docker Compose configuration
โ”œโ”€โ”€ Dockerfile                  # Docker image build settings
โ”œโ”€โ”€ requirements.txt            # Dependency package list
โ””โ”€โ”€ .env.example                # Example environment variables file

4. Architecture Design

4.1 Layered Structure

  1. Interface Layer: MCP Server (FastMCP)
  2. Business Logic Layer: Handlers and Executors
  3. Data Access Layer: Database connection and query execution

4.2 Key Classes and Modules

  • MySQLMCPServer: Main server class that initializes and runs the MCP server
  • DatabaseManager: Singleton pattern-based database connection manager
  • Executors: Collection of tools for database operations
    • execute_create_table: Creates tables
    • execute_desc_table: Checks table schema
    • execute_explain: Provides query execution plans
    • execute_insert_query: Executes INSETR queries
    • execute_select_query: Executes SELECT queries
    • execute_show_tables: Retrieves table lists

4.3 Communication Flow

  1. AI model requests a list of available tools from the MCP server.
  2. The server returns the available tools list.
  3. The AI model requests the execution of a specific tool.
  4. The server calls the corresponding executor to perform the database operation.
  5. The execution results are returned to the AI model.

5. Scalability and Maintenance

  • Adding Tools: Implement new tools in the executors directory and register them in __init__.py.
  • Environment Configuration: Manage environment variables via the .env file.
  • Logging: Ensure consistent logging using logger_helper.

6. Deployment and Execution

6.1 Local Execution

# Setup environment
cp .env.example .env
# Modify .env file as needed

# Install dependencies
pip install -r requirements.txt

# Run the server
python mysql_mcp_server/main.py run

6.2 Docker Deployment

# Start database using Docker Compose
docker-compose up -d db
# Build and run mysql-mcp-server with Docker Compose (including rebuilds)
docker-compose up -d --build mysql-mcp-server

7. Security Considerations

  • Manage database credentials via environment variables.
  • Use strong passwords in production environments.
  • Consider implementing SSL/TLS encryption for database connections when necessary.

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

iflow_mcp_mineru98_mysql_mcp_server-1.0.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file iflow_mcp_mineru98_mysql_mcp_server-1.0.0.tar.gz.

File metadata

  • Download URL: iflow_mcp_mineru98_mysql_mcp_server-1.0.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_mineru98_mysql_mcp_server-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a4424df00e404ffa17a8211e7861b783df57c7f940f9668bb1617e8b1855c4b3
MD5 20ea184ff668b6292ebbaf5f8a2da8e3
BLAKE2b-256 6c6c43ff5b1dbc45d93702da58b263361f747a17221bd404246db2e3e0fc24c9

See more details on using hashes here.

File details

Details for the file iflow_mcp_mineru98_mysql_mcp_server-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_mineru98_mysql_mcp_server-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_mineru98_mysql_mcp_server-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 835274b776ac02c36b544d6d462e576a2b17f5a260e776a73a3fe2f951558ad6
MD5 9782060dcfa6aa9df63878eebf064fad
BLAKE2b-256 46ca9b07d07a0f0849c20c20b0eede18624b7ed5158ad29e75604db295049e40

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