ddonathan/mysql-mcp-server
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.
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:
| Variable | Description | Default |
|---|---|---|
MYSQL_HOST | MySQL server hostname | localhost |
MYSQL_PORT | MySQL server port | 3306 |
MYSQL_USER | MySQL username | root |
MYSQL_PASS | MySQL password | (empty) |
MYSQL_DB | Database 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 executeparams(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 nameType: Data type (e.g., "varchar(255)", "int")Null: Whether NULL is allowedKey: Key type (PRI, UNI, MUL)Default: Default valueExtra: 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 nameTABLE_ROWS: Approximate row countSize_MB: Table size in megabytesENGINE: 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 versionconnection_id: Current connection IDdatabase: Current databaseuptime_seconds: Server uptimethreads_connected: Active connectionsmax_connections: Connection limit
mysql_get_database_stats
Get database overview statistics.
Parameters: None
Returns:
database_name: Current databasetotal_tables: Number of tablestotal_size_mb: Total size in MBserver_version: MySQL versioncharacter_set: Default charsetcollation: Default collation
Security
- SQL Injection Prevention: All user-supplied values should be passed via the
paramsarray, 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_healthtool for diagnostics - Added
mysql_get_database_statstool for database overview - Added
mysql_list_procedurestool - Improved error messages with actionable suggestions
- Renamed entry point to
index.tsfollowing MCP conventions
v1.0.0
- Initial release with basic query, show tables, describe, and table info tools
License
MIT