Varada-Raja-Reddy-Peddyreddy/mysql-mcp-server
3.2
If you are the rightful owner of mysql-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
A Model Context Protocol (MCP) server that enables AI agents to safely interact with MySQL databases through a standardized interface.
Tools
3
Resources
0
Prompts
0
MySQL MCP Server
High-Level Architecture (Simple)
flowchart TD
Client[MCP Client]
Server[MCP Server]
DB[(MySQL Database)]
Client -- API Request --> Server
Server -- SQL Query --> DB
DB -- Query Result --> Server
Server -- API Response --> Client
A Model Context Protocol (MCP) server that enables AI agents to safely interact with MySQL databases through a standardized interface.
šļø Architecture Overview
This project implements a client-server architecture using the MCP protocol, allowing AI agents to query MySQL databases securely.
graph TB
subgraph "AI Agent/Client"
A[AI Agent] --> B[MCP Client]
B --> C[JSON-RPC Messages]
end
subgraph "MCP Server Layer"
C --> D[FastMCP Framework]
D --> E[Tool Registry]
E --> F[list_tables]
E --> G[describe_table]
E --> H[read_only_query]
end
subgraph "Database Layer"
F --> I[MySQL Connector]
G --> I
H --> I
I --> J[MySQL Database]
subgraph "Security Layer"
K[Environment Variables]
L[Read-Only Validation]
M[SQL Injection Protection]
end
I --> K
H --> L
H --> M
end
subgraph "Communication Protocol"
N[STDIO Transport]
O[JSON-RPC 2.0]
P[Async Communication]
end
D --> N
N --> O
O --> P
style A fill:#e1f5fe
style D fill:#f3e5f5
style I fill:#e8f5e8
style J fill:#fff3e0
style N fill:#fce4ec
š§© Components Breakdown
1. MCP Client (mcp_client.py
)
- Purpose: Demonstrates how to connect to and interact with the MCP server
- Key Features:
- Establishes connection via STDIO transport
- Sends JSON-RPC messages for initialization
- Lists available tools and calls them
- Handles async communication with the server
2. MCP Server (server.py
)
- Purpose: Main server implementation that handles client requests
- Key Features:
- FastMCP framework integration
- Tool registration and management
- Database connection handling
- Security validation
3. Database Tools
list_tables()
- Function: Returns all tables in the connected database
- SQL:
SHOW TABLES;
- Output: Simple list of table names
describe_table(table_name)
- Function: Shows schema information for a specific table
- SQL:
DESCRIBE table_name;
- Output: Formatted table structure with columns, types, keys, etc.
read_only_query(query)
- Function: Executes safe SELECT queries only
- Security: Validates query starts with "SELECT"
- Output: Formatted table results
4. Database Connection (get_db_connection()
)
- Purpose: Manages MySQL database connections
- Features:
- Environment variable configuration
- Error handling and connection pooling
- Automatic cleanup of resources
5. Configuration (secrect_api_keys.py
)
- Purpose: Stores API keys and configuration
- Note: This file should be added to
.gitignore
in production
š§ Installation & Setup
Prerequisites
- Python 3.8+
- MySQL Server
- pip package manager
Step 1: Clone and Setup
git clone <your-repo>
cd MCP
python -m venv venv
Step 2: Activate Virtual Environment
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activate
Step 3: Install Dependencies
pip install -r requirements.txt
Step 4: Environment Configuration
Create a .env
file:
DB_HOST=localhost
DB_USER=your_username
DB_PASSWORD=your_password
DB_NAME=your_database
DB_PORT=3306
š Usage
Starting the Server
python server.py
Running the Client
python mcp_client.py
Example Client-Server Interaction
- Initialization: Client sends
initialize
request - Tool Discovery: Client requests available tools via
tools/list
- Tool Execution: Client calls specific tools with
tools/call
- Response: Server returns formatted results
š Security Features
Query Validation
- Only SELECT queries are allowed
- SQL injection protection through parameterized queries
- Read-only access enforced at application level
Connection Security
- Environment variable configuration
- No hardcoded credentials
- Automatic connection cleanup
Error Handling
- Graceful failure handling
- No sensitive information exposure
- Connection timeout management
š Data Flow
sequenceDiagram
participant AI as AI Agent
participant Client as MCP Client
participant Server as MCP Server
participant DB as MySQL Database
AI->>Client: Request database info
Client->>Server: Initialize connection
Server->>Client: Connection established
Client->>Server: List available tools
Server->>Client: Return tool list
Client->>Server: Call list_tables()
Server->>DB: SHOW TABLES
DB->>Server: Table list
Server->>Client: Formatted results
Client->>AI: Display table names
AI->>Client: Request table schema
Client->>Server: Call describe_table(table_name)
Server->>DB: DESCRIBE table_name
DB->>Server: Schema information
Server->>Client: Formatted schema
Client->>AI: Display table structure
š ļø Development
Adding New Tools
- Create a new function in
server.py
- Decorate with
@mcp.tool()
- Add proper error handling
- Update documentation
Testing
# Test database connection
python -c "from server import get_db_connection; print(get_db_connection())"
# Test tool execution
python mcp_client.py
š Project Structure
MCP/
āāā server.py # Main MCP server implementation
āāā mcp_client.py # Example client for testing
āāā requirements.txt # Python dependencies
āāā secrect_api_keys.py # Configuration file
āāā README.md # This file
āāā venv/ # Virtual environment
š® Future Enhancements
- Add more database operations (INSERT, UPDATE, DELETE with proper validation)
- Implement connection pooling for better performance
- Add support for other database types (PostgreSQL, SQLite)
- Create web-based admin interface
- Add authentication and user management
- Implement query caching and optimization
- Add comprehensive logging and monitoring
š¤ Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
š Resources
š License
This project is licensed under the MIT License - see the LICENSE file for details.
š Support
If you encounter any issues or have questions:
- Check the error logs
- Verify your database connection
- Ensure all dependencies are installed
- Open an issue in the repository
Happy coding! š