MCP-MySQL-Ops
Copilot instructions2 starsChanged 15 months agoMITView on GitHub
This is a Model Context Protocol (MCP) server built with FastMCP that provides MySQL database monitoring and operations through natural language queries. The server acts as a safe, read-only bridge between AI assistants and MySQL databases. Multi-Database Architecture: All tools accept optional database_name parameter to target specific databases while maintaining a default connection database from MYSQL_DATABASE env var. Performance Schema Integration: Core functionality leverages MySQL's Performance Schema and Information Schema. Tools automatically detect available features and adapt accordingly. Version-Aware Tools:…
- Reads credentials
The file
Show its rules and text
We split this copilot-instructions.md into the rules it gives an agent
15 rules. About 0 are common, 6 are distinctive, and 9 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
9
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 16
Multi-Database Architecture: All tools accept optional database_name parameter to target specific databases while maintaining a default …
- Very specific: line 20
Version-Aware Tools: Use version_compat.py for MySQL 5.7.9+/8.0+ compatibility.
- Very specific: line 68
Use host.docker.internal for MYSQL_HOST when connecting from containers to host MySQL
- Very specific: line 83
All MCP tools must return str (never raise exceptions to MCP layer)
- Very specific: line 88
Use format_table_data(results, title) for consistent table output
- Distinctive: line 93
When adding new tools, must update the compatibility matrix in README.md:
- Distinctive: line 95
Document MySQL version support (5.7.9+/8.0+)
- Distinctive: line 97
Update tool count statistics
- Very specific: line 163
Run ./scripts/create-test-data.sh to create 4 realistic databases:
- Distinctive: line 170
Test tools with realistic prompts - never use function names directly:
- Distinctive: line 182
Performance Schema Assumptions: Check Performance Schema availability before using enhanced monitoring tools
- Very specific: line 184
Environment Loading: Use scripts/run-mcp-inspector-local.sh which properly loads .env - direct Python execution won't load environment
- Distinctive: line 185
Query Limits: All tools enforce 1-100 limits for performance.
- Very specific: line 186
aiomysql Parameter Binding: Use %s format, not $1, $2, ...
- Very specific: line 188
Version-Specific Features: Always use version_compat.py patterns for new tools that query system tables
≈ “Common” is an estimate: at least 1 in 5 of the 1,644 Copilot instructions files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
MCP MySQL Operations Server - AI Coding Agent Instructions
Architecture Overview
This is a Model Context Protocol (MCP) server built with FastMCP that provides MySQL database monitoring and operations through natural language queries. The server acts as a safe, read-only bridge between AI assistants and MySQL databases.
Core Components
mcp_main.py: Main MCP server with 19+@mcp.tool()decorated functionsfunctions.py: Database connection layer usingaiomysqlwith multi-database supportversion_compat.py: MySQL 5.7.9+ and 8.0+ version detection and adaptive feature handling (MIN_SUPPORTED_VERSION = 5.7.9is enforced viaensure_min_supported_version)prompt_template.md: Comprehensive prompt definitions loaded via@mcp.prompt()decorators- Docker stack: MySQL + MCP server + MCPO proxy + Open WebUI integration
Key Patterns
Multi-Database Architecture: All tools accept optional database_name parameter to target specific databases while maintaining a default connection database from MYSQL_DATABASE env var.
Performance Schema Integration: Core functionality leverages MySQL's Performance Schema and Information Schema. Tools automatically detect available features and adapt accordingly.
Version-Aware Tools: Use version_compat.py for MySQL 5.7.9+/8.0+ compatibility. Tools auto-adapt features based on detected version, and ensure_min_supported_version() raises UnsupportedMySQLVersionError for servers older than 5.7.9 (which lack performance_schema.processlist). MySQL 8.0+ recommended for enhanced capabilities.
Tool Structure: Each MCP tool follows this pattern:
@mcp.tool()
async def get_something(limit: int = 20, database_name: str = None) -> str:
"""Detailed docstring with [Tool Purpose], [Exact Functionality], [Required Use Cases], [Strictly Prohibited Use Cases]"""
try:
# Validate inputs (limit constraints: max 1-100)
# Execute queries via functions.py
# Return formatted table data
except Exception as e:
logger.error(f"Failed to...: {e}")
return f"Error: {str(e)}"Information Schema Column Casing: Critical MySQL compatibility pattern - always use uppercase column names:
# Correct - MySQL returns uppercase column names
SELECT ENGINE, TABLE_NAME FROM information_schema.tables
# Handle both cases safely in code
engine_name = result.get('ENGINE') or result.get('engine', 'Unknown')Development Workflows
Local Development
# Primary development command - loads .env, starts MCP Inspector
./scripts/run-mcp-inspector-local.sh
# Direct execution for debugging with custom log levels
python -m src.mcp_mysql_ops.mcp_main --log-level DEBUG --type streamable-httpDocker Development
# Full stack with MySQL + test data
docker-compose up -d
# Test data generation (creates 4 databases with realistic test data)
./scripts/create-test-data.shEnvironment Configuration
- Copy
.env.exampleto.envand modify connection parameters - Critical:
MYSQL_DATABASEserves dual purpose - default connection target AND Docker database creation name - Use
host.docker.internalforMYSQL_HOSTwhen connecting from containers to host MySQL
Code Conventions
Database Connection Pattern
# Multi-database support - database parameter overrides MYSQL_CONFIG default
async def get_db_connection(database: str = None) -> aiomysql.Connection:
config = MYSQL_CONFIG.copy()
if database:
config["db"] = database # Override default
return await aiomysql.connect(**config)Error Handling
- All MCP tools must return
str(never raise exceptions to MCP layer) - Log errors with
logger.error()then return user-friendly error messages - Mask sensitive information in connection info with
sanitize_connection_info()
Query Formatting
- Use
format_table_data(results, title)for consistent table output - Apply
format_bytes()andformat_duration()for human-readable values - Enforce limit constraints:
limit = max(1, min(limit, 100))
Tool Compatibility Matrix
When adding new tools, must update the compatibility matrix in README.md:
- Classify as Professional MySQL Tools (19 total) or Performance Schema Enhanced Tools
- Document MySQL version support (5.7.9+/8.0+)
- List Information Schema/Performance Schema tables used
- Update tool count statistics
Recent Major Changes
- PostgreSQL-Style Init System: Automatic test data generation using dedicated
mysql-init-datacontainer - 19 Professional Tools: Expanded from 11 to 19 specialized monitoring tools
- Enhanced Docker Dependencies:
service_completed_successfullypattern for proper startup sequencing - User Permission Management: Root-to-user delegation system for secure test data access
Project-Specific Integrations
Prompt Template System
The server loads prompts from prompt_template.md using a custom parsing system:
@mcp.prompt("prompt_template_full")- complete template@mcp.prompt("prompt_template_headings")- section list onlyget_prompt_template(section="specific_section")- targeted content
Docker Multi-Service Architecture
- mysql: MySQL 8.0 (version from
MYSQL_VERSIONenv var, default 8.0) - mysql-init-data: One-shot container that creates comprehensive test data
- mcp-server: Main MCP server container
- mcpo-proxy: HTTP proxy for web-based MCP clients
- open-webui: Web interface for testing
Container Startup Dependencies: Uses service_completed_successfully pattern:
# open-webui waits for test data generation to complete
depends_on:
mysql-init-data:
condition: service_completed_successfullyCritical Dependencies
# Required for all database operations
import aiomysql # Not PyMySQL - uses aiomysql exclusively for async operations
# MCP framework
from fastmcp import FastMCP
# Version checks are recommended for enhanced features
version = await get_mysql_version()MySQL Version Compatibility Patterns
The major work focuses on MySQL version compatibility. Key patterns:
Information Schema vs Performance Schema: MySQL 8.0+ has enhanced Performance Schema tables while 5.7 uses basic ones:
# In version_compat.py
if version.supports_enhanced_performance_schema: # MySQL 8.0+
query = "SELECT * FROM performance_schema.events_statements_summary_by_digest"
else: # MySQL 5.7
query = "SELECT * FROM information_schema.tables"Feature Detection: MySQL 8.0+ has additional features like JSON functions, CTEs, window functions:
if version.supports_json_functions: # MySQL 8.0+
json_columns = ["JSON_EXTRACT(data, '$.field') as extracted_field"]
else: # MySQL 5.7
json_columns = ["NULL as extracted_field"]Testing Strategy
Test Data Generation
- Run
./scripts/create-test-data.shto create 4 realistic databases:test_ecommerce: E-commerce with products, orders, customerstest_analytics: Web analytics and sales datatest_inventory: Warehouse managementtest_hr: Employee and payroll data
Natural Language Testing
Test tools with realistic prompts - never use function names directly:
- ✅ "Show me the current server status"
- ❌ "Run get_server_status"
Configuration Files
mcp-config.json.stdio: Standard CLI integrationmcp-config.json.http: HTTP mode for web clients- Both must have matching port configurations with
docker-compose.yml
Common Pitfalls
- Database Parameter Confusion:
MYSQL_DATABASEis the default connection database, not a constraint on which databases can be accessed - Performance Schema Assumptions: Check Performance Schema availability before using enhanced monitoring tools
- Port Misalignment:
FASTMCP_PORT, Docker external port, and MCP config files must all match - Environment Loading: Use
scripts/run-mcp-inspector-local.shwhich properly loads.env- direct Python execution won't load environment - Query Limits: All tools enforce 1-100 limits for performance; don't assume unlimited results
- aiomysql Parameter Binding: Use
%sformat, not$1, $2, ...- all SQL queries must use aiomysql-compatible parameter binding - MySQL Version Support: Currently supports 5.7.9+/8.0+; latest versions support pending testing
- Version-Specific Features: Always use
version_compat.pypatterns for new tools that query system tables
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

