FastMCP MySQL Server
Provides tools for executing SQL queries against a MySQL database, with support for prepared statements, security features (injection detection, rate limiting), and optional write operations.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@FastMCP MySQL Servershow me the users table schema"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
FastMCP MySQL Server
A FastMCP server implementation for MySQL database operations, providing secure and efficient access to MySQL databases for LLM applications.
Features
š Secure by Default: Read-only access with optional write permissions
ā” High Performance: Connection pooling and async operations
š”ļø SQL Injection Protection: Built-in query validation and prepared statements
š Comprehensive Monitoring: Structured JSON logging
š§ Flexible Configuration: Environment variable based configuration
š Easy Deployment: Install and run with
uvx
Related MCP server: Bun Database MCP Server
Installation
Using uvx (Recommended)
# Run directly with uvx
uvx fastmcp-mysql
# With environment variables
MYSQL_HOST=localhost MYSQL_USER=myuser MYSQL_PASSWORD=mypass MYSQL_DB=mydb uvx fastmcp-mysqlUsing pip
pip install fastmcp-mysqlFrom source
git clone https://github.com/jinto/fastmcp-mysql
cd fastmcp-mysql
uv sync --all-extrasConfiguration
Configure the server using environment variables:
Required Variables
Variable | Description | Default |
| Database username | - |
| Database password | - |
Optional Variables
Variable | Description | Default |
| Database host | "127.0.0.1" |
| Database port | "3306" |
| Database name (optional) | None |
| Enable INSERT queries | false |
| Enable UPDATE queries | false |
| Enable DELETE queries | false |
| Connection pool size | 10 |
| Query timeout (ms) | 30000 |
| Log level (DEBUG, INFO, WARNING, ERROR) | INFO |
| Enable query result caching | true |
| Maximum cache entries | 1000 |
| Cache TTL (ms) | 60000 |
| Cache eviction policy (lru/ttl/fifo) | lru |
| Cache cleanup interval (seconds) | 60.0 |
| Cache invalidation strategy | aggressive |
| Streaming query chunk size | 1000 |
| Default page size | 10 |
| Maximum page size | 1000 |
Usage
Claude Desktop Configuration
Using Claude MCP CLI (Recommended)
# Install from PyPI (when published)
claude mcp add fastmcp-mysql \
-e MYSQL_HOST="127.0.0.1" \
-e MYSQL_PORT="3306" \
-e MYSQL_USER="your_username" \
-e MYSQL_PASSWORD="your_password" \
-e MYSQL_DB="your_database" \
-- uvx fastmcp-mysql
# Without specifying a database (use USE command)
claude mcp add fastmcp-mysql \
-e MYSQL_HOST="127.0.0.1" \
-e MYSQL_USER="your_username" \
-e MYSQL_PASSWORD="your_password" \
-- uvx fastmcp-mysql
# For local development
claude mcp add fastmcp-mysql \
-e MYSQL_HOST="127.0.0.1" \
-e MYSQL_PORT="3306" \
-e MYSQL_USER="your_username" \
-e MYSQL_PASSWORD="your_password" \
-e MYSQL_DB="your_database" \
-- uv run --project /path/to/fastmcp-mysql fastmcp-mysqlManual Configuration
Add to your Claude Desktop configuration file:
{
"mcpServers": {
"mysql": {
"command": "uvx",
"args": ["fastmcp-mysql"],
"env": {
"MYSQL_HOST": "localhost",
"MYSQL_PORT": "3306",
"MYSQL_USER": "your_username",
"MYSQL_PASSWORD": "your_password",
"MYSQL_DB": "your_database",
"MYSQL_ENABLE_SECURITY": "true",
"MYSQL_RATE_LIMIT_RPM": "60",
"MYSQL_RATE_LIMIT_BURST": "10"
}
}
}
}Available Tools
mysql_query
Execute SQL queries against the configured MySQL database.
Parameters:
query(string, required): The SQL query to executeparams(array, optional): Query parameters for prepared statementsdatabase(string, optional): Target database (for multi-db mode)
Example:
# Simple query
result = await mysql_query("SELECT * FROM users WHERE active = 1")
# With parameters (SQL injection safe)
result = await mysql_query(
"SELECT * FROM users WHERE age > %s AND city = %s",
params=[18, "New York"]
)
# When no database is specified initially
result = await mysql_query("USE mydb")
result = await mysql_query("SHOW TABLES")
result = await mysql_query("SHOW DATABASES")Security
Default Security Features
FastMCP MySQL includes comprehensive security features:
Read-only by default: Write operations must be explicitly enabled
SQL injection prevention:
Advanced pattern detection for SQL injection attempts
Parameter validation for all queries
Detection of encoded injection attempts (URL, Unicode, Hex)
Query filtering:
Blacklist mode: Blocks dangerous operations (DDL, system tables, file operations)
Whitelist mode: Only allows explicitly approved query patterns
Customizable filtering rules
Rate limiting:
Per-user request throttling
Configurable algorithms (Token Bucket, Sliding Window, Fixed Window)
Burst protection
Security Configuration
Configure security features via environment variables:
Variable | Description | Default |
| Enable all security features | true |
| Enable SQL injection detection | true |
| Enable rate limiting | true |
| Filter mode (blacklist/whitelist/combined) | blacklist |
| Rate limit requests per minute | 60 |
| Burst size for rate limiting | 10 |
| Rate limiting algorithm (token_bucket/sliding_window/fixed_window) | token_bucket |
| Maximum query length in characters | 10000 |
| Maximum parameter length | 1000 |
| Log security violations | true |
| Log rejected queries | true |
| Audit all queries (performance impact) | false |
Enabling Write Operations
Write operations are disabled by default. Enable them with caution:
# Enable specific write operations
MYSQL_ALLOW_INSERT=true \
MYSQL_ALLOW_UPDATE=true \
MYSQL_ALLOW_DELETE=true \
uvx fastmcp-mysqlSecurity Best Practices
Use Prepared Statements: Always use parameters instead of string concatenation
Principle of Least Privilege: Only enable write operations when necessary
Monitor Security Events: Check logs for security violations
Rate Limiting: Adjust limits based on your application needs
Whitelist Mode: Use whitelist mode for production environments when possible
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/jinto/fastmcp-mysql
cd fastmcp-mysql
# Create virtual environment with uv
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
uv sync --all-extras
# Install pre-commit hooks
pre-commit installRunning Tests
# Run all tests
uv run pytest tests/
# Run with coverage
uv run pytest tests/ --cov=fastmcp_mysql
# Run specific test file
uv run pytest tests/unit/test_query.py
# Run integration tests only
uv run pytest tests/integration/Code Quality
# Format code
uv run black src tests
# Lint code
uv run ruff check src tests
# Type checking
uv run mypy srcArchitecture
The server follows Clean Architecture principles:
src/fastmcp_mysql/
āāā __init__.py # Package initialization
āāā __main__.py # Entry point for uvx
āāā config.py # Configuration management
āāā server.py # FastMCP server setup
āāā connection.py # Database connection management
āāā security/ # Security module (Clean Architecture)
ā āāā __init__.py
ā āāā manager.py # Security orchestration
ā āāā config.py # Security configuration
ā āāā exceptions.py # Security exceptions
ā āāā interfaces/ # Abstract interfaces
ā ā āāā injection_detector.py
ā ā āāā query_filter.py
ā ā āāā rate_limiter.py
ā āāā injection/ # SQL injection detection
ā ā āāā detector.py
ā ā āāā patterns.py
ā āāā filtering/ # Query filtering
ā ā āāā blacklist.py
ā ā āāā whitelist.py
ā ā āāā combined.py
ā āāā rate_limiting/ # Rate limiting
ā āāā token_bucket.py
ā āāā sliding_window.py
ā āāā fixed_window.py
ā āāā factory.py
āāā tools/ # MCP tools
āāā __init__.py
āāā query.py # Query execution toolContributing
Fork the repository
Create a feature branch (
git checkout -b feature/amazing-feature)Commit your changes (
git commit -m 'Add amazing feature')Push to the branch (
git push origin feature/amazing-feature)Open a Pull Request
Please ensure:
All tests pass
Code is formatted with black
Type hints are added
Documentation is updated
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
Based on the MCP Server MySQL Node.js implementation
Built with FastMCP framework
MySQL connectivity via aiomysql
Support
š Documentation
š Issue Tracker
š¬ Discussions
This server cannot be deployed
Maintenance
Related MCP Connectors
Draxlr's remote MCP server connects AI assistants to your SQL databases and dashboards. Explore schemas, run read-only queries, manage saved queries and dashboards, and export results, all with row-level security so each user sees only their own data.
- dataOAuthco.thinair
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
- mcpOAuthcom.gibsonai
GibsonAI MCP server: manage your databases with natural language
The Remote MCP server acts as a standardized bridge between LLM applications (like Claude, ChatGPT, and Cursor) and external services, enabling AI agents to access external tools and resources. Its primary capability is providing a centralized search tool to discover other MCP servers and their respective tools. Unlike local implementations, it runs remotely with OAuth authentication and permission controls for security.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceAn MCP server that integrates with MySQL databases, enabling secure read and write operations through LLM-driven interfaces with support for transaction handling and performance monitoring.27 npm18MIT
- FlicenseNot gradedqualityDmaintenanceA high-performance MCP server that enables AI assistants to safely interact with MySQL databases through secure CRUD operations, schema inspection, and parameterized queries with built-in SQL injection prevention.1-
- AlicenseAqualityBmaintenanceA lightweight MySQL MCP server that enables LLMs to interact with databases through tools for schema inspection and query execution. It features LLM-friendly formatting, SSL support, and a secure read-only mode with query timeout protections.7344 npmMIT
- AlicenseNot gradedqualityDmaintenanceA MySQL MCP server for secure database interaction, enabling schema inspection, query execution, and RBAC via AI coding assistants.618 npm5MIT