MCP Server for MySQL based on NodeJS


A Model Context Protocol server that provides access to MySQL databases. This server enables LLMs to inspect database schemas and execute SQL queries.
Table of Contents
Requirements
- Node.js v18 or higher
- MySQL 5.7 or higher (MySQL 8.0+ recommended)
- MySQL user with appropriate permissions for the operations you need
- For write operations: MySQL user with INSERT, UPDATE, and/or DELETE privileges
Installation
There are several ways to install and configure the MCP server:
Claude Desktop
To manually configure the MCP server for Claude Desktop App, add the following to your claude_desktop_config.json
file (typically located in your user directory):
{
"mcpServers": {
"mcp_server_mysql": {
"command": "npx",
"args": [
"-y",
"@benborla29/mcp-server-mysql"
],
"env": {
"MYSQL_HOST": "127.0.0.1",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASS": "your_password",
"MYSQL_DB": "your_database",
"ALLOW_INSERT_OPERATION": "false",
"ALLOW_UPDATE_OPERATION": "false",
"ALLOW_DELETE_OPERATION": "false",
"PATH": "/Users/atlasborla/Library/Application Support/Herd/config/nvm/versions/node/v22.9.0/bin:/usr/bin:/bin",
"NODE_PATH": "/Users/atlasborla/Library/Application Support/Herd/config/nvm/versions/node/v22.9.0/lib/node_modules"
}
}
}
}
Cursor
For Cursor IDE, you can install this MCP server with the following command in your project:
npx mcprunner MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306 MYSQL_USER=root MYSQL_PASS=root MYSQL_DB=demostore ALLOW_INSERT_OPERATION=true ALLOW_UPDATE_OPERATION=true ALLOW_DELETE_OPERATION=false -- npx -y @benborla29/mcp-server-mysql
Don't forget to replace the env
values on that command. If you have the latest version (for v0.47 and above) of Cursor, just copy and paste the config below:
mcp.json
{
"mcpServers": {
"MySQL": {
"command": "npx",
"args": [
"mcprunner",
"MYSQL_HOST=127.0.0.1",
"MYSQL_PORT=3306",
"MYSQL_USER=root",
"MYSQL_PASS=root",
"MYSQL_DB=demostore",
"ALLOW_INSERT_OPERATION=true",
"ALLOW_UPDATE_OPERATION=true",
"ALLOW_DELETE_OPERATION=false",
"--",
"npx",
"-y",
"@benborla29/mcp-server-mysql"
]
}
}
}
Using Smithery
The easiest way to install and configure this MCP server is through Smithery:
npx -y @smithery/cli@latest install @benborla29/mcp-server-mysql --client claude
During configuration, you'll be prompted to enter your MySQL connection details. Smithery will automatically:
- Set up the correct environment variables
- Configure your LLM application to use the MCP server
- Test the connection to your MySQL database
- Provide helpful troubleshooting if needed
- Configure write operation settings (INSERT, UPDATE, DELETE permissions)
The installation will ask for the following connection details:
- MySQL Host (default: 127.0.0.1)
- MySQL Port (default: 3306)
- MySQL Username
- MySQL Password
- MySQL Database name
- SSL Configuration (if needed)
- Write operations permissions:
- Allow INSERT operations (default: false)
- Allow UPDATE operations (default: false)
- Allow DELETE operations (default: false)
For security reasons, write operations are disabled by default. Enable them only if you need Claude to modify your database data.
Using MCP Get
You can also install this package using MCP Get:
npx @michaellatman/mcp-get@latest install @benborla29/mcp-server-mysql
MCP Get provides a centralized registry of MCP servers and simplifies the installation process.
Using NPM/PNPM
For manual installation:
npm install -g @benborla29/mcp-server-mysql
pnpm add -g @benborla29/mcp-server-mysql
After manual installation, you'll need to configure your LLM application to use the MCP server (see Configuration section below).
Running from Local Repository
If you want to clone and run this MCP server directly from the source code, follow these steps:
-
Clone the repository
git clone https://github.com/benborla/mcp-server-mysql.git
cd mcp-server-mysql
-
Install dependencies
npm install
pnpm install
-
Build the project
npm run build
pnpm run build
-
Configure Claude Desktop
Add the following to your Claude Desktop configuration file (claude_desktop_config.json
):
{
"mcpServers": {
"mcp_server_mysql": {
"command": "/path/to/node",
"args": [
"/full/path/to/mcp-server-mysql/dist/index.js"
],
"env": {
"MYSQL_HOST": "127.0.0.1",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASS": "your_password",
"MYSQL_DB": "your_database",
"ALLOW_INSERT_OPERATION": "false",
"ALLOW_UPDATE_OPERATION": "false",
"ALLOW_DELETE_OPERATION": "false",
"PATH": "/Users/atlasborla/Library/Application Support/Herd/config/nvm/versions/node/v22.9.0/bin:/usr/bin:/bin",
"NODE_PATH": "/Users/atlasborla/Library/Application Support/Herd/config/nvm/versions/node/v22.9.0/lib/node_modules"
}
}
}
}
Replace:
/path/to/node
with the full path to your Node.js binary (find it with which node
)
/full/path/to/mcp-server-mysql
with the full path to where you cloned the repository
- Set the MySQL credentials to match your environment
-
Test the server
node dist/index.js
If it connects to MySQL successfully, you're ready to use it with Claude Desktop.
Components
Tools
- mysql_query
- Execute SQL queries against the connected database
- Input:
sql
(string): The SQL query to execute
- By default, limited to READ ONLY operations
- Optional write operations (when enabled via configuration):
- INSERT: Add new data to tables (requires
ALLOW_INSERT_OPERATION=true
)
- UPDATE: Modify existing data (requires
ALLOW_UPDATE_OPERATION=true
)
- DELETE: Remove data (requires
ALLOW_DELETE_OPERATION=true
)
- All operations are executed within a transaction with proper commit/rollback handling
- Supports prepared statements for secure parameter handling
- Configurable query timeouts and result pagination
- Built-in query execution statistics
Resources
The server provides comprehensive database information:
- Table Schemas
- JSON schema information for each table
- Column names and data types
- Index information and constraints
- Foreign key relationships
- Table statistics and metrics
- Automatically discovered from database metadata
Security Features
- SQL injection prevention through prepared statements
- Query whitelisting/blacklisting capabilities
- Rate limiting for query execution
- Query complexity analysis
- Configurable connection encryption
- Read-only transaction enforcement
Performance Optimizations
- Optimized connection pooling
- Query result caching
- Large result set streaming
- Query execution plan analysis
- Configurable query timeouts
Monitoring and Debugging
- Comprehensive query logging
- Performance metrics collection
- Error tracking and reporting
- Health check endpoints
- Query execution statistics
Configuration
Automatic Configuration with Smithery
If you installed using Smithery, your configuration is already set up. You can view or modify it with:
smithery configure @benborla29/mcp-server-mysql
When reconfiguring, you can update any of the MySQL connection details as well as the write operation settings:
For security reasons, all write operations are disabled by default. Only enable these settings if you specifically need Claude to modify your database data.
Advanced Configuration Options
For more control over the MCP server's behavior, you can use these advanced configuration options:
{
"mcpServers": {
"mcp_server_mysql": {
"command": "/path/to/npx/binary/npx",
"args": [
"-y",
"@benborla29/mcp-server-mysql"
],
"env": {
"MYSQL_HOST": "127.0.0.1",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASS": "",
"MYSQL_DB": "db_name",
"PATH": "/path/to/node/bin:/usr/bin:/bin",
"MYSQL_POOL_SIZE": "10",
"MYSQL_QUERY_TIMEOUT": "30000",
"MYSQL_CACHE_TTL": "60000",
"MYSQL_RATE_LIMIT": "100",
"MYSQL_MAX_QUERY_COMPLEXITY": "1000",
"MYSQL_SSL": "true",
"MYSQL_ENABLE_LOGGING": "true",
"MYSQL_LOG_LEVEL": "info",
"MYSQL_METRICS_ENABLED": "true",
"ALLOW_INSERT_OPERATION": "false",
"ALLOW_UPDATE_OPERATION": "false",
"ALLOW_DELETE_OPERATION": "false"
}
}
}
}
Environment Variables
Basic Connection
MYSQL_HOST
: MySQL server host (default: "127.0.0.1")
MYSQL_PORT
: MySQL server port (default: "3306")
MYSQL_USER
: MySQL username (default: "root")
MYSQL_PASS
: MySQL password
MYSQL_DB
: Target database name (leave empty for multi-DB mode)
Performance Configuration
MYSQL_POOL_SIZE
: Connection pool size (default: "10")
MYSQL_QUERY_TIMEOUT
: Query timeout in milliseconds (default: "30000")
MYSQL_CACHE_TTL
: Cache time-to-live in milliseconds (default: "60000")
Security Configuration
MYSQL_RATE_LIMIT
: Maximum queries per minute (default: "100")
MYSQL_MAX_QUERY_COMPLEXITY
: Maximum query complexity score (default: "1000")
MYSQL_SSL
: Enable SSL/TLS encryption (default: "false")
ALLOW_INSERT_OPERATION
: Enable INSERT operations (default: "false")
ALLOW_UPDATE_OPERATION
: Enable UPDATE operations (default: "false")
ALLOW_DELETE_OPERATION
: Enable DELETE operations (default: "false")
ALLOW_DDL_OPERATION
: Enable DDL operations (default: "false")
SCHEMA_INSERT_PERMISSIONS
: Schema-specific INSERT permissions
SCHEMA_UPDATE_PERMISSIONS
: Schema-specific UPDATE permissions
SCHEMA_DELETE_PERMISSIONS
: Schema-specific DELETE permissions
SCHEMA_DDL_PERMISSIONS
: Schema-specific DDL permissions
MULTI_DB_WRITE_MODE
: Enable write operations in multi-DB mode (default: "false")
Monitoring Configuration
MYSQL_ENABLE_LOGGING
: Enable query logging (default: "false")
MYSQL_LOG_LEVEL
: Logging level (default: "info")
MYSQL_METRICS_ENABLED
: Enable performance metrics (default: "false")
Multi-DB Mode
MCP-Server-MySQL supports connecting to multiple databases when no specific database is set. This allows the LLM to query any database the MySQL user has access to. For full details, see README-MULTI-DB.md.
Enabling Multi-DB Mode
To enable multi-DB mode, simply leave the MYSQL_DB
environment variable empty. In multi-DB mode, queries require schema qualification:
SELECT * FROM database_name.table_name;
USE database_name;
SELECT * FROM table_name;
Schema-Specific Permissions
For fine-grained control over database operations, MCP-Server-MySQL now supports schema-specific permissions. This allows different databases to have different levels of access (read-only, read-write, etc.).
Configuration Example
SCHEMA_INSERT_PERMISSIONS=development:true,test:true,production:false
SCHEMA_UPDATE_PERMISSIONS=development:true,test:true,production:false
SCHEMA_DELETE_PERMISSIONS=development:false,test:true,production:false
SCHEMA_DDL_PERMISSIONS=development:false,test:true,production:false
For complete details and security recommendations, see README-MULTI-DB.md.
Testing
Database Setup
Before running tests, you need to set up the test database and seed it with test data:
-
Create Test Database and User
CREATE DATABASE IF NOT EXISTS mcp_test;
CREATE USER IF NOT EXISTS 'mcp_test'@'localhost' IDENTIFIED BY 'mcp_test_password';
GRANT ALL PRIVILEGES ON mcp_test.* TO 'mcp_test'@'localhost';
FLUSH PRIVILEGES;
-
Run Database Setup Script
pnpm run setup:test:db
This will create the necessary tables and seed data. The script is located in scripts/setup-test-db.ts
-
Configure Test Environment
Create a .env.test
file in the project root (if not existing):
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_USER=mcp_test
MYSQL_PASS=mcp_test_password
MYSQL_DB=mcp_test
-
Update package.json Scripts
Add these scripts to your package.json:
{
"scripts": {
"setup:test:db": "ts-node scripts/setup-test-db.ts",
"pretest": "pnpm run setup:test:db",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage"
}
}
Running Tests
The project includes a comprehensive test suite to ensure functionality and reliability:
pnpm run setup:test:db
pnpm test
Troubleshooting
Common Issues
{
"env": {
"PATH": "/path/to/node/bin:/usr/bin:/bin"
}
}
Where can I find my node
bin path
Run the following command to get it:
For PATH
echo "$(which node)/../"
For NODE_PATH
echo "$(which node)/../../lib/node_modules"
npx -y -p @benborla29/mcp-server-mysql -p dotenv mcp-server-mysql
Thanks to @lizhuangs
Contributing
Contributions are welcome! Please feel free to submit a Pull Request to
https://github.com/benborla/mcp-server-mysql
Development Setup
- Clone the repository
- Install dependencies:
pnpm install
- Build the project:
pnpm run build
- Run tests:
pnpm test
Project Roadmap
We're actively working on enhancing this MCP server. Check our CHANGELOG.md for details on planned features, including:
- Enhanced query capabilities with prepared statements
- Advanced security features
- Performance optimizations
- Comprehensive monitoring
- Expanded schema information
If you'd like to contribute to any of these areas, please check the issues on GitHub or open a new one to discuss your ideas.
Submitting Changes
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin feature/your-feature-name
- Submit a pull request
License
This MCP server is licensed under the MIT License. See the LICENSE file for details.