@ahmedmustafamalik/mysql-mcp
v1.2.3
Published
MCP server for MySQL database interactions
Downloads
70
Maintainers
Readme
MySQL MCP Server
A Model Context Protocol (MCP) server for MySQL database interactions. This server allows LLMs to understand database schemas and execute safe SQL queries with built-in security features and scalability support.
🚀 Quick Start: New to this project? Check out our Getting Started Guide for the fastest way to get up and running!
Features
- Schema Analysis: Comprehensive database schema exploration including tables, views, and stored procedures
- Safe Query Execution: Built-in SQL injection protection and read-only query validation
- Connection Pooling: Efficient connection management with configurable pool settings
- Docker Support: Ready-to-deploy Docker container with multi-stage builds
- TypeScript Support: Full TypeScript implementation with type safety
- AWS RDS Compatible: Optimized for AWS RDS MySQL instances with SSL support
- Scalable Architecture: Designed for high-performance database interactions
Installation
Local Development
# Clone the repository
git clone https://github.com/Ahmustufa/mysql-mcp.git
cd mysql-mcp
# Install dependencies
npm install
# Install development dependencies (if not automatically installed)
npm install --save-dev tsx @types/nodeDocker Installation
# Build the Docker image
docker build -t mysql-mcp .
# Or pull from registry (if published)
docker pull ahmustufa/mysql-mcp:latestConfiguration
Environment Variables
Create a .env file in the project root:
# Required Database Configuration
DB_SERVER=localhost
DB_DATABASE=your_database_name
DB_USER=your_username
DB_PASSWORD=your_password
# Optional Configuration
DB_PORT=3306
DB_SSL=false # Set to 'false' to disable SSL, or leave undefined for AWS RDS SSL
# For AWS RDS MySQL
# DB_SERVER=your-rds-endpoint.region.rds.amazonaws.com
# DB_SSL=Amazon RDS # Enables SSL for RDSDocker Environment
For Docker deployments, you can use environment variables or a .env file:
# Docker with environment variables
docker run -e DB_SERVER=localhost \
-e DB_DATABASE=mydb \
-e DB_USER=user \
-e DB_PASSWORD=password \
mysql-mcp
# Docker with .env file
docker run --env-file .env mysql-mcpUsage
Development Mode
npm run devProduction Mode
npm run build
npm startDocker Usage
# Basic Docker run
docker run --env-file .env mysql-mcp
# Docker with port mapping (if extending with HTTP endpoints)
docker run -p 3000:3000 --env-file .env mysql-mcp
# Docker Compose (see docker-compose.yml section below)
docker-compose up -dAvailable Tools
1. execute_query
Execute SQL queries with built-in safety validation.
Parameters:
query(string): SQL query to execute
Safety Features:
- Blocks destructive operations (DROP, DELETE, TRUNCATE)
- Prevents SQL injection attacks
- Read-focused operations
Example:
{
"query": "SELECT * FROM users WHERE id = 1"
}2. list_tables
List all base tables in the current database.
Parameters: None
Returns: Array of table information including schema, name, and type.
Example Usage:
{
"tool": "list_tables"
}3. describe_table
Get detailed schema information for a specific table.
Parameters:
table_name(string): Name of the table to describe
Returns: Column information including data types, nullability, defaults, and constraints.
Example:
{
"table_name": "users"
}4. get_table_data
Retrieve sample data from a table.
Parameters:
table_name(string): Name of the tablelimit(number, optional): Number of rows to return (1-100, default: 10)
Example:
{
"table_name": "users",
"limit": 5
}5. primary_keys
Get primary key column(s) for a specific table.
Parameters:
table_name(string): Name of the table
Returns: List of primary key columns for the specified table.
Example:
{
"table_name": "users"
}6. search_columns
Find columns whose names match a pattern (case-insensitive).
Parameters:
like(string): Pattern to search for in column names
Returns: List of matching columns across all tables with their data types.
Example:
{
"like": "email"
}7. foreign_keys
List foreign key relationships for a table.
Parameters:
table_name(string): Name of the table
Returns: Foreign key relationships both referencing and referenced by the table.
Example:
{
"table_name": "posts"
}8. introspect_schema
Get a complete schema overview of the database.
Parameters: None
Returns: Comprehensive JSON map of all tables with their columns, primary keys, and foreign key relationships.
Example:
{
"tool": "introspect_schema"
}Security Features
- SQL Injection Protection: Advanced pattern detection and query validation
- Read-Only Default: Destructive operations are blocked by default
- Connection Pooling: Secure and efficient database connections
- SSL Support: Built-in SSL support for secure database connections
- Parameter Validation: All inputs are validated before execution
- Query Length Limits: Protection against DoS attacks
Docker Support
Basic Docker Commands
# Build the image
docker build -t mysql-mcp .
# Run with environment variables
docker run --rm \
-e DB_SERVER=your-mysql-server \
-e DB_DATABASE=your_database \
-e DB_USER=your_user \
-e DB_PASSWORD=your_password \
mysql-mcp
# Run with .env file
docker run --rm --env-file .env mysql-mcp
# Run with volume mount for configuration
docker run --rm \
-v $(pwd)/.env:/app/.env \
mysql-mcpDocker Compose
Create a docker-compose.yml file:
version: "3.8"
services:
mysql-mcp:
build: .
environment:
- DB_SERVER=mysql
- DB_DATABASE=testdb
- DB_USER=root
- DB_PASSWORD=rootpassword
- DB_PORT=3306
depends_on:
- mysql
restart: unless-stopped
mysql:
image: mysql:8.0
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_DATABASE=testdb
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
restart: unless-stopped
volumes:
mysql_data:Run with Docker Compose:
# Start services
docker-compose up -d
# View logs
docker-compose logs -f mysql-mcp
# Stop services
docker-compose down
# Stop and remove volumes
docker-compose down -vScaling with Docker
For high-availability deployments:
version: "3.8"
services:
mysql-mcp:
build: .
deploy:
replicas: 3
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
environment:
- DB_SERVER=mysql-primary
- DB_DATABASE=proddb
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
depends_on:
- mysql-primary
mysql-primary:
image: mysql:8.0
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=proddb
volumes:
- mysql_primary_data:/var/lib/mysql
deploy:
placement:
constraints: [node.role == manager]
volumes:
mysql_primary_data:Error Handling
The server includes comprehensive error handling:
- Database connection errors
- Query validation errors
- SQL execution errors
- Parameter validation errors
- Graceful shutdown handling
Database Schema Information
The server provides detailed schema information including:
- Tables: Columns, data types, constraints, and structure
- Views: Available database views and their definitions
- Data Types: MySQL-specific data types and constraints
- Indexes: Primary keys and index information
- Character Sets: UTF-8 and collation support
Environment Variables
| Variable | Description | Default | Required |
| ------------- | --------------------------- | ------------ | -------- |
| DB_SERVER | MySQL server hostname or IP | - | ✅ |
| DB_DATABASE | Database name | - | ✅ |
| DB_USER | Database username | - | ✅ |
| DB_PASSWORD | Database password | - | ✅ |
| DB_PORT | Database port | 3306 | ❌ |
| DB_SSL | SSL configuration | 'Amazon RDS' | ❌ |
SSL Configuration
- Set
DB_SSL=falseto disable SSL completely - Leave undefined or set to
'Amazon RDS'for AWS RDS SSL support - For custom SSL configurations, modify the connection settings in the code
AWS RDS Integration
This MCP server is optimized for AWS RDS MySQL instances:
# AWS RDS Configuration
DB_SERVER=your-rds-instance.region.rds.amazonaws.com
DB_DATABASE=your_database
DB_USER=admin
DB_PASSWORD=your_secure_password
DB_PORT=3306
# DB_SSL is automatically set to 'Amazon RDS' for SSLRDS Security Groups
Ensure your RDS security group allows connections from your application:
- Port: 3306 (or your custom port)
- Source: Your application's IP range or security group
Performance and Scalability
Connection Pooling
The server uses MySQL2's built-in connection pooling:
- Automatic connection management
- Connection reuse for better performance
- Configurable timeout settings (60 seconds default)
Scaling Strategies
- Horizontal Scaling: Deploy multiple MCP server instances
- Database Read Replicas: Point to MySQL read replicas for read-heavy workloads
- Connection Limits: Monitor and configure MySQL
max_connections - Resource Monitoring: Use CloudWatch or similar for RDS monitoring
Production Optimization
# Multi-stage build for smaller production images
FROM node:22.20-alpine as builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
FROM node:22.20-alpine as production
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY build ./build
CMD ["node", "build/index.js"]Development
Project Structure
mysql-mcp/
├── src/
│ └── index.ts # Main entry point with MCP server implementation
├── build/ # Compiled TypeScript output
├── .env # Environment configuration
├── .dockerignore # Docker ignore file
├── Dockerfile # Docker container configuration
├── docker-compose.yml # Docker Compose setup (create this)
├── package.json # Node.js dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── README.md # This documentationDevelopment Commands
# Install dependencies
npm install
# Start development server with hot reload
npm run dev
# Build for production
npm run build
# Start production server
npm start
# Watch for changes (compile only)
npm run watch
# Clean build directory
npm run cleanAdding New Tools
To add new MCP tools, register them in src/index.ts:
server.registerTool(
"your_tool_name",
{
title: "Your Tool Title",
description: "Description of what your tool does",
inputSchema: {
parameter: z.string().min(1),
},
},
async ({ parameter }) => {
// Your tool implementation
const result = await performSomeOperation(parameter);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
);Error Handling
The server includes comprehensive error handling:
- Database Connection Errors: Automatic retry with exponential backoff
- Query Validation Errors: Detailed error messages for invalid queries
- SQL Execution Errors: Safe error reporting without exposing sensitive data
- Parameter Validation: Type checking and sanitization
- Graceful Shutdown: Proper cleanup of database connections
Troubleshooting
Common Issues
Connection Refused
Error: connect ECONNREFUSED- Check if MySQL server is running
- Verify DB_SERVER and DB_PORT configuration
- Check firewall/security group settings
Authentication Failed
Error: Access denied for user- Verify DB_USER and DB_PASSWORD
- Check user permissions in MySQL
- Ensure user can connect from your host
Database Not Found
Error: Unknown database- Verify DB_DATABASE exists
- Check user has access to the specified database
SSL Connection Issues
Error: SSL connection error- For local development: set
DB_SSL=false - For RDS: ensure SSL certificates are properly configured
- For local development: set
Debug Mode
Enable detailed logging by setting NODE_ENV:
NODE_ENV=development npm run devTesting
Unit Tests
# Add testing framework
npm install --save-dev jest @types/jest
# Run tests
npm testIntegration Tests
# Test with actual database
npm run test:integrationLoad Testing
# Test connection pooling and performance
npm run test:loadContributing
We welcome contributions! Please follow these guidelines:
Getting Started
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name - Make your changes with proper TypeScript types
- Add tests for new functionality
- Update documentation as needed
- Submit a pull request
Code Style
- Use TypeScript for all new code
- Follow ESLint and Prettier configurations
- Add JSDoc comments for public APIs
- Use semantic commit messages
Pull Request Process
- Ensure all tests pass
- Update the README if needed
- Add a clear description of changes
- Reference any related issues
Changelog
v1.1.0
- Switched from MSSQL to MySQL support
- Added AWS RDS SSL compatibility
- Improved connection pooling
- Added Docker support with multi-stage builds
- Enhanced security with query validation
v1.0.0
- Initial release with basic MCP tools
- TypeScript implementation
- Basic Docker support
License
Apache License 2.0 - see LICENSE file for details.
Support
For issues and questions:
- GitHub Issues: Open an issue
- Documentation: Check this README for common solutions
- Community: Join discussions in GitHub Discussions
Related Projects
Acknowledgments
- Model Context Protocol team for the excellent SDK
- MySQL2 maintainers for the robust database driver
- The open-source community for continuous improvements
