MySQL MCP Server

MySQL MCP Server

A Model Context Protocol server that provides secure, multi-database MySQL access with configurable security levels, enabling SQL queries across multiple databases directly from VS Code.

Category
访问服务器

README

MySQL MCP Server

A sophisticated Model Context Protocol (MCP) server that provides secure, multi-database MySQL access with configurable security levels, lazy loading, and hot reload capabilities.

🚀 Features

  • 🗄️ Multi-Database Support: Configure and access multiple MySQL databases simultaneously
  • 🔒 Configurable Security: Four security levels from read-only to full access
  • ⚡ Lazy Loading: Connection pools created only when needed for optimal resource usage
  • 🔄 Hot Reload: Database configuration updates without server restart
  • 📊 Complete Schema Information: Full table definitions with indexes, foreign keys, and constraints
  • 🛡️ Explicit Database Selection: Required database parameter prevents accidental operations
  • 🎯 MCP Protocol Integration: Native VS Code integration with resource browsing

🔧 Installation

  1. Clone and Install

    git clone <your-repository-url>
    cd mysql-mcp-server
    npm install
    
  2. Configure Multi-Database Setup

    Copy the example configuration file and customize it:

    cp .env.example .env
    

    Edit .env with your database configurations:

    # Multi-Database Configuration (JSON Array)
    MYSQL_DATABASES='[
      {
        "name": "primary_db",
        "host": "localhost",
        "user": "your_username",
        "password": "your_password",
        "database": "your_database"
      },
      {
        "name": "analytics_db", 
        "host": "analytics.example.com",
        "user": "analytics_user",
        "password": "analytics_password",
        "database": "analytics"
      }
    ]'
    
    # Security Configuration
    # Options: (default), extended, all, or custom comma-separated list
    # MYSQL_ALLOWED_COMMANDS=extended
    
    # Connection Pool Settings
    MYSQL_CONNECTION_LIMIT=4
    MYSQL_WAIT_FOR_CONNECTIONS=true
    
    # Server Metadata
    MCP_SERVER_NAME=mysql-mcp-server
    MCP_SERVER_VERSION=1.0.0
    
  3. Add to VS Code MCP Configuration

    Add to your VS Code settings.json under MCP servers:

    {
      "mcp": {
        "mcpServers": {
          "mysql-mcp-server": {
            "command": "node",
            "args": ["C:/path/to/mysql-mcp-server/mcp-mysql-lite.js"],
            "env": {}
          }
        }
      }
    }
    

🔒 Security Levels

Configure MYSQL_ALLOWED_COMMANDS in your .env file:

Default (Recommended)

# MYSQL_ALLOWED_COMMANDS not set or commented out

Allowed Commands: SELECT, SHOW, DESCRIBE, DESC, EXPLAIN, ANALYZE
Use Case: Safe read-only access for data analysis and exploration

Extended

MYSQL_ALLOWED_COMMANDS=extended

Allowed Commands: All default commands plus CREATE TABLE, ALTER TABLE, DROP TABLE, INSERT, UPDATE, DELETE, TRUNCATE, etc.
Use Case: Development environments where schema and data modifications are needed

All (⚠️ Use with Extreme Caution)

MYSQL_ALLOWED_COMMANDS=all

Allowed Commands: No restrictions - all SQL commands permitted
Use Case: Trusted environments requiring full database administration capabilities

Custom

MYSQL_ALLOWED_COMMANDS="SELECT,INSERT,UPDATE,CREATE TABLE"

Allowed Commands: User-defined comma-separated list
Use Case: Specific operational requirements with tailored permissions

🛠️ Available Tools

All tools require explicit database selection for security.

query_database

Execute SQL queries with security validation and explicit database selection.

  • Parameters:
    • sql (string, required) - The SQL query to execute
    • database (string, required) - Database name to target (security requirement)
  • Security: Commands validated against current security level
  • Returns: Query results with execution metadata

describe_table

Get complete table structure using SHOW CREATE TABLE for comprehensive schema information.

  • Parameters:
    • table_name (string, required) - Name of the table to describe
    • database (string, required) - Database containing the table (security requirement)
  • Returns: Full table definition including columns, indexes, foreign keys, and engine details

list_databases

Display all configured databases and their connection status.

  • Parameters: None required
  • Returns: Database configurations with connection pool status and default database indicator

📚 Available Resources

Tables from the default database are exposed as MCP resources with URIs like:

mysql://database_name/table/table_name

Browse table schemas directly through VS Code's resource explorer.

💡 Usage Examples

Basic Database Query

-- Query with explicit database selection (security requirement)
SELECT COUNT(*) FROM users WHERE active = 1

Database: primary_db

Multi-Database Operations

-- Analytics query on different database
SELECT DATE(created_at), COUNT(*) 
FROM events 
WHERE event_type = 'purchase' 
GROUP BY DATE(created_at) 
ORDER BY DATE(created_at) DESC 
LIMIT 7

Database: analytics_db

Table Schema Exploration

Use describe_table tool:

  • Table: users
  • Database: primary_db

Returns complete table definition with foreign keys, indexes, and constraints.

Database Discovery

Use list_databases tool to see all configured databases and their connection status:

[
  {
    "name": "primary_db",
    "host": "localhost", 
    "database": "app_production",
    "user": "app_user",
    "isDefault": true,
    "poolCreated": true
  },
  {
    "name": "analytics_db",
    "host": "analytics.company.com",
    "database": "analytics",
    "user": "analytics_user", 
    "isDefault": false,
    "poolCreated": false
  }
]

🔄 Hot Reload Feature

Update database configurations in .env file - they'll be automatically reloaded:

  • Credential updates: New passwords/users applied immediately
  • New databases: Added to available list (restart VS Code to update tool schemas)
  • Configuration changes: Host/port updates applied on next connection
  • 🔄 Existing connections: Remain active until naturally recycled

⚡ Lazy Loading

Connection pools are created only when needed:

  • 📊 Resource efficient: No unnecessary database connections
  • 🚀 Fast startup: Server starts immediately regardless of database availability
  • 🔍 Status visibility: list_databases shows which pools are active
  • 🛡️ Failure isolation: One database issue doesn't affect others

🏗️ Architecture

VS Code MCP Client
       ↓ (stdio)
MySQL MCP Server
       ↓ (lazy loading)
Connection Pools
       ↓ (MySQL protocol)  
Multiple Databases

Key design principles:

  • Multi-Database: Each database has independent configuration and connection pool
  • Security: Command validation before execution, explicit database selection required
  • Performance: Lazy loading + connection pooling for optimal resource usage
  • Reliability: Hot reload + error isolation for production stability

🚀 Development

Running the Server

npm start

Testing Configuration

# Test connection to all configured databases
node mcp-mysql-lite.js

Environment Variables

  • MYSQL_DATABASES: JSON array of database configurations
  • MYSQL_ALLOWED_COMMANDS: Security level (default/extended/all/custom)
  • MYSQL_CONNECTION_LIMIT: Max connections per pool (default: 4)
  • MYSQL_WAIT_FOR_CONNECTIONS: Wait for available connections (default: true)

📄 License

MIT License - see LICENSE file for details.


Built with ❤️ for the Model Context Protocol ecosystem

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选