mysql-mcp-server

ddonathan/mysql-mcp-server

3.2

If you are the rightful owner of mysql-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

Model Context Protocol server for MySQL database access.

Tools
4
Resources
0
Prompts
0

MySQL MCP Server

Model Context Protocol server for MySQL database access. Provides secure, validated tools for querying and exploring MySQL databases.

Features

  • Secure Query Execution: Prepared statements with parameterized queries to prevent SQL injection
  • Schema Exploration: List tables, describe schemas, view stored procedures
  • Database Statistics: Table sizes, row counts, connection health monitoring
  • Runtime Validation: All inputs validated with Zod schemas
  • Actionable Errors: Clear, helpful error messages with suggestions

Installation

npm install
npm run build

Configuration

Configure via environment variables:

VariableDescriptionDefault
MYSQL_HOSTMySQL server hostnamelocalhost
MYSQL_PORTMySQL server port3306
MYSQL_USERMySQL usernameroot
MYSQL_PASSMySQL password(empty)
MYSQL_DBDatabase name(empty)

Usage with Claude Code

Add to your MCP configuration (e.g., .claude/.mcp.json):

{
  "mcpServers": {
    "mysql": {
      "command": "node",
      "args": ["/path/to/mysql-mcp-server/dist/index.js"],
      "env": {
        "MYSQL_HOST": "your-host",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "your-user",
        "MYSQL_PASS": "your-password",
        "MYSQL_DB": "your-database"
      }
    }
  }
}

Available Tools

mysql_query

Execute SQL queries with prepared statement support.

Parameters:

  • query (string, required): The SQL query to execute
  • params (array, optional): Parameters for prepared statements

Examples:

// Simple SELECT
{ "query": "SELECT * FROM users LIMIT 10" }

// With parameters (prevents SQL injection)
{ "query": "SELECT * FROM users WHERE status = ? AND age > ?", "params": ["active", 18] }

// INSERT with parameters
{ "query": "INSERT INTO logs (message) VALUES (?)", "params": ["Test log"] }

// JOIN query
{ "query": "SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id" }

mysql_show_tables

List all tables in the current database.

Parameters: None

Returns: Array of table names

mysql_describe_table

Get the schema/structure of a specific table.

Parameters:

  • table (string, required): Table name to describe

Returns:

  • Field: Column name
  • Type: Data type (e.g., "varchar(255)", "int")
  • Null: Whether NULL is allowed
  • Key: Key type (PRI, UNI, MUL)
  • Default: Default value
  • Extra: Additional info (e.g., "auto_increment")

mysql_table_info

Get detailed table statistics including row counts and sizes.

Parameters:

  • limit (number, optional): Max tables to return (default: 50, max: 1000)

Returns:

  • TABLE_NAME: Table name
  • TABLE_ROWS: Approximate row count
  • Size_MB: Table size in megabytes
  • ENGINE: Storage engine (InnoDB, MyISAM, etc.)
  • TABLE_COLLATION: Character set collation

mysql_list_procedures

List stored procedures in the database.

Parameters:

  • schema (string, optional): Filter by schema name

Returns: Array of procedure metadata

mysql_connection_health

Check database connection health and diagnostics.

Parameters: None

Returns:

  • status: "healthy" or "unhealthy"
  • server_version: MySQL version
  • connection_id: Current connection ID
  • database: Current database
  • uptime_seconds: Server uptime
  • threads_connected: Active connections
  • max_connections: Connection limit

mysql_get_database_stats

Get database overview statistics.

Parameters: None

Returns:

  • database_name: Current database
  • total_tables: Number of tables
  • total_size_mb: Total size in MB
  • server_version: MySQL version
  • character_set: Default charset
  • collation: Default collation

Security

  • SQL Injection Prevention: All user-supplied values should be passed via the params array, never concatenated into queries
  • Parameterized Queries: The server uses MySQL2's prepared statement support
  • Input Validation: All tool inputs are validated with Zod schemas before execution
  • Error Sanitization: Error messages don't expose internal stack traces

Development

# Install dependencies
npm install

# Build TypeScript
npm run build

# Development with auto-reload
npm run dev

# Run the server
npm start

Changelog

v2.0.0

  • Upgraded to MCP SDK v1.6.1 with modern McpServer.tool() API
  • Added Zod schema validation for all inputs
  • Fixed SQL injection vulnerability in mysql_table_info
  • Added mysql_connection_health tool for diagnostics
  • Added mysql_get_database_stats tool for database overview
  • Added mysql_list_procedures tool
  • Improved error messages with actionable suggestions
  • Renamed entry point to index.ts following MCP conventions

v1.0.0

  • Initial release with basic query, show tables, describe, and table info tools

License

MIT