A secure FastMCP implementation that enables LLMs to interact with MySQL databases through natural language queries.
- ✅ Secure database connections with connection pooling
- ✅ SQL injection prevention and query validation
- ✅ Automatic schema analysis and metadata extraction
- ✅ Natural language to SQL query generation
- ✅ Safe query execution with parameterized statements
- ✅ Real-time streaming of large result sets
- ✅ Schema visualization support (Mermaid.js and PlantUML)
- ✅ Full MCP protocol compliance
- ✅ Context-aware error reporting
- ✅ Thread-safe server instance management
- ✅ Resource cleanup and connection management
- ✅ SQL injection prevention
- ✅ Input validation for queries
- ✅ Parameterized queries support
- ✅ Connection pooling with timeout
- ✅ Resource cleanup on errors
- ✅ Thread-safe server instances
- ✅ Safe schema analysis
- Install dependencies:
pip install -r requirements.txt- Configure your database connection:
# Create database configuration
config = DatabaseConfig(
host="localhost",
port=3306,
user="your_username",
password="your_password",
database="your_database"
)python mcp_server.pyThe server will start on port 8000.
- Create a file named
mcp_servers.jsonin your project directory:
{
"mcpServers": {
"database": {
"command": "uv",
"args": [
"--directory",
"/ABSOLUTE/PATH/TO/PARENT/FOLDER/db-mcp-windsurf",
"run",
"mcp_server.py"
]
}
}
}- Make sure the MCP server is running (from step 1)
- In Claude Desktop, you can now use the MCP server by referencing it as
database://in your conversations.
In your Claude conversation, configure the database connection:
config = {
"host": "your_host",
"port": 3306,
"user": "your_username",
"password": "your_password",
"database": "your_database",
"read_only": true
}Once configured, you can use the MCP server in any Claude conversation by referencing it as database://. For example:
# Connect to database
await database.tools.connect_database(config)
# Generate SQL from natural language
await database.tools.generate_sql("Find all users who joined in the last 30 days")
# Execute SQL query
await database.tools.execute_query("SELECT * FROM users LIMIT 10")
# Get database schema
await database.read_resource("schema://database")
# Get schema description
await database.read_resource("schema://description")
# Get ER diagram
await database.read_resource("schema://er-diagram/mermaid")
# Validate SQL query
await database.tools.validate_query("SELECT * FROM users")
# Disconnect from database
await database.tools.disconnect_database(config)The MCP server is configured to run with uvicorn using the following command:
uvicorn mcp_server:mcp --host 0.0.0.0 --port 8000 --reloadThis will start the MCP server on port 8000 with hot-reload enabled for development.
Once configured, you can use the MCP server in any Claude conversation by referencing it as database://. For example:
# Connect to database
await database.tools.connect_database(config)
# Generate SQL from natural language
await database.tools.generate_sql("Find all users who joined in the last 30 days")
# Execute SQL query
await database.tools.execute_query("SELECT * FROM users LIMIT 10")
# Get database schema
await database.read_resource("schema://database")
# Get schema description
await database.read_resource("schema://description")
# Get ER diagram
await database.read_resource("schema://er-diagram/mermaid")
# Validate SQL query
await database.tools.validate_query("SELECT * FROM users")
# Disconnect from database
await database.tools.disconnect_database(config)Once configured, you can use the plugin in any Claude conversation by referencing it as database://. For example:
# Connect to database
await database.tools.connect_database(config)
# Generate SQL from natural language
await database.tools.generate_sql("Find all users who joined in the last 30 days")
# Execute SQL query
await database.tools.execute_query("SELECT * FROM users LIMIT 10")
# Get database schema
await database.read_resource("schema://database")
# Get schema description
await database.read_resource("schema://description")
# Get ER diagram
await database.read_resource("schema://er-diagram/mermaid")
# Validate SQL query
await database.tools.validate_query("SELECT * FROM users")
# Disconnect from database
await database.tools.disconnect_database(config)Once configured, you can use the plugin in any Claude conversation. The plugin provides the following capabilities:
-
Natural Language to SQL
- Ask Claude questions about your database
- The plugin will automatically generate and execute SQL queries
- Results will be displayed in a readable format
-
Schema Analysis
- Get detailed database schema information
- View table relationships and dependencies
- Generate ER diagrams
-
Query Validation
- All queries are validated against your schema
- Read-only mode protection
- SQL injection prevention
-
Performance Optimization
- Automatic query optimization
- Proper indexing suggestions
- Performance monitoring
You can use these commands directly in Claude conversations:
# Connect to database
await mcp.tools.connect_database(config)
# Generate SQL from natural language
await mcp.tools.generate_sql("Find all users who joined in the last 30 days")
# Execute SQL query
await mcp.tools.execute_query("SELECT * FROM users LIMIT 10")
# Get database schema
await mcp.read_resource("schema://database")
# Get schema description
await mcp.read_resource("schema://description")
# Get ER diagram
await mcp.read_resource("schema://er-diagram/mermaid")
# Validate SQL query
await mcp.tools.validate_query("SELECT * FROM users")
# Disconnect from database
await mcp.tools.disconnect_database(config)- Connect Database
# Connect to database
await mcp.tools.connect_database(config)- Disconnect Database
# Clean up resources
await mcp.tools.disconnect_database(config)- Generate SQL
# Generate SQL from natural language
await mcp.tools.generate_sql("Show me total sales per category")- Execute Query
# Execute query with safety
await mcp.tools.execute_query("SELECT * FROM products LIMIT 10")- Get Schema
# Get database schema
await mcp.read_resource("schema://database")- Get Schema Description
# Get human-readable schema description
await mcp.read_resource("schema://description")- Get ER Diagram
# Get ER diagram in Mermaid format
await mcp.read_resource("schema://er-diagram/mermaid")- Validate Query
# Validate SQL query against schema
await mcp.tools.validate_query("SELECT * FROM products")The server automatically analyzes the connected database and extracts:
- Table names and columns with detailed info (type, null, key, default, extra)
- Primary and foreign key relationships
- Indexes (names, involved columns, uniqueness)
- Table relationships (inferred from foreign keys)
- Table and column comments
- Schema visualization capabilities
The server implements comprehensive error handling:
- Context-aware error reporting
- Detailed error messages
- Automatic logging
- Input validation
- Safe database operations
- Resource cleanup on errors
- Thread safety
- Connection pooling management
The project uses FastMCP for MCP protocol implementation and SQLAlchemy for database interactions. Key features include:
- Async/await support
- Context-based logging
- Resource templates
- Tool-based architecture
- Extensible error handling
- Schema visualization support
- Secure query execution
- Thread-safe operations
- Resource cleanup
- Always validate input before execution
- Use parameterized queries for all database operations
- Implement proper connection pooling
- Clean up resources when done
- Use thread-safe operations
- Validate schema against queries
- Implement proper error handling
- Use connection timeouts and recycling
- Implement query validation
- Use secure database configurations
- Use connection pooling for better performance
- Implement proper query optimization
- Use LIMIT clauses for large result sets
- Implement proper indexing
- Use proper data types
- Implement proper caching strategies
- Use streaming for large result sets
- Implement proper connection timeout
- Use proper connection recycling
- Implement proper resource cleanup