Universal MCP
An intelligent database gateway that provides secure database access via MCP protocol and natural language queries using OpenAI's GPT models.
README
Universal MCP: An Intelligent Database Gateway
Universal MCP is an intelligent database gateway designed to provide secure and intelligent access to various databases. It supports both direct SQL execution via the MCP protocol and natural language queries (Text-to-SQL) through a REST API, leveraging OpenAI's GPT models for advanced natural language processing capabilities.
ποΈ Architecture
The server architecture integrates an intelligence layer directly, offering a hybrid approach to data access. This design allows for flexible interaction with physical databases while incorporating advanced AI functionalities.
βββββββββββββββββββ MCP Protocol ββββββββββββββββββββββββββββββββββββ
β β (HTTP+SSE) β β
β MCP Clients βββββββββββββββββββββΊβ Universal MCP Server β
β (VSCode, etc.) β β β
βββββββββββββββββββ β ββββββββββββββββββββββββββββββββ β
β β Intelligence β β
βββββββββββββββββββ REST API β β β β
β β β β β’ NLP-to-SQL (via REST API) β β
β REST Clients ββββββββββββββββββββββΊβ β β’ LLM Integration (ChatGPT) β β
β (Web Apps, etc.)β β β β’ Conversation Memory β β
βββββββββββββββββββ β β β’ SQL Generation & Validationβ β
β ββββββββββββββββββββββββββββββββ β
β ββββββββββββββββββββββββββββββββ β
β β Core Services & Tools β β
β β β β
β β β’ Schema Management β β
β β β’ Secure Query Executor β β
β β β’ Multi-DB Support β β
β βββββββββββββββββ¬βββββββββββββββ β
ββββββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββ
β Physical Databases β
β (PostgreSQL, MySQL, SQLite, etc.)β
ββββββββββββββββββββββββββββββββββββ
π― Purpose
Universal MCP functions as a hybrid database gateway with the following core purposes:
- Secure REST API: Provides an interface for converting natural language questions into SQL queries.
- Conversation Management: Manages conversation history to support contextual, multi-turn questions.
- Secure SQL Execution: Offers secure and validated SQL execution via the traditional MCP protocol.
- Schema Generation: Generates database schema YAML for tool configuration and LLM context.
- Multi-Database Support: Supports various database types through a flexible adapter pattern.
β¨ New Feature: Natural Language Queries (Text-to-SQL)
The server now incorporates a robust REST API and an interactive terminal client that utilizes ChatGPT to translate natural language into SQL queries. Key aspects of this feature include:
- Conversational Memory: The system maintains conversation context using a persistent file-based session store, enabling users to ask follow-up questions.
- Secure by Design: All LLM-generated queries undergo a security validation process, ensuring that only safe
SELECTstatements are executed. - Interactive Terminal Client: A user-friendly terminal interface (
scripts/chat.py) facilitates full conversational interaction with the database.
π Quick Start (Local Setup)
This guide outlines the steps to run the application directly on a local machine (macOS/Linux).
1. Prerequisites
- Python 3.11+
- A running local MySQL server (installed via Homebrew or standard installer).
2. Local MySQL Setup (macOS via Homebrew)
If you don't have MySQL installed:
# Install MySQL
brew install mysql
# Start the service
brew services start mysql
# Secure installation (optional but recommended)
mysql_secure_installation
3. Setup Environment
git clone <repository-url>
cd universal-mcp
# Create and activate virtual environment
python -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
4. Configure Environment
# Copy environment template
cp .env.example .env
# Edit .env and add your local database credentials and OpenAI API key
nano .env
Important: Ensure OPENAI_API_KEY is added and DB_HOST, DB_USER, and DB_PASSWORD match your local MySQL setup.
5. Initialize the Database
This script creates necessary tables and populates them with sample data in your local MySQL database.
First, ensure the database and user exist. You can run this command (replace YOUR_PASSWORD with the password from your .env file):
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS company_db; CREATE USER IF NOT EXISTS 'mcp_server_user'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD'; GRANT ALL PRIVILEGES ON company_db.* TO 'mcp_server_user'@'localhost'; FLUSH PRIVILEGES;"
Then, run the setup script to create tables and sample data:
python -m scripts.setup_database
6. Start the Server
This command starts the FastAPI server, which provides the REST API.
# Leave this terminal running
uvicorn api.main:app --reload --port 8000
7. Chat with Your Database
Open a new terminal window and start the interactive chat client.
# Make sure your virtual environment is activated
source venv/bin/activate
# Run the chat script
python scripts/chat.py
π§ Available Tools & APIs
Natural Language API
| Endpoint | Method | Description |
|---|---|---|
/api/v1/query/natural-language |
POST |
Converts a natural language question to a SQL query and executes it. |
Traditional MCP Tools
| Tool | Description |
|---|---|
execute_query |
Execute validated SQL queries safely. |
get_schema |
Retrieve complete database schema. |
list_tables |
Get list of available tables. |
describe_table |
Get detailed table structure. |
get_table_data |
Retrieve paginated data from tables. |
validate_query |
Validate SQL syntax and security. |
Using Pagination
The get_table_data tool supports pagination for efficient handling of large tables. Use the page and page_size parameters to request specific data chunks.
π Monitoring & Observability
How to Access Logs
Logs are crucial for debugging and can be accessed in two ways:
- Real-time Console Output: All logs are streamed directly to the terminal where the
uvicornserver is running. - Persistent Log File: The server also writes all logs to
mcp_server.login the project's root directory.
# Watch the log file for new entries in real-time
tail -f mcp_server.log
The log verbosity can be controlled via the LOG_LEVEL variable in your .env file (options: DEBUG, INFO, WARNING, ERROR).
π Project Structure (Highlights)
universal-mcp/
βββ mcp_server.log # Main log file
βββ config/
β βββ settings.py # Loads configs, including OPENAI_API_KEY
βββ core/
β βββ security/
β β βββ query_validator.py # Security check for LLM queries
β βββ mcp/
β βββ tools/
β βββ natural_language_query.py # Orchestrates NLQ
βββ api/
β βββ main.py # Main FastAPI application
β βββ query_routes.py # Defines the NLQ endpoint
βββ services/
β βββ llm_service.py # Handles all interaction with OpenAI API
βββ storage/
β βββ session/
β βββ context_store.py # Manages file-based session memory
βββ scripts/
β βββ setup_database.py # Initializes the local database
β βββ chat.py # The interactive terminal chat client
βββ tests/
βββ unit/
β βββ test_query_validator.py
βββ integration/
βββ test_nlq_endpoint.py
π Security Features
- LLM Query Validation: All generated SQL queries are validated to be
SELECT-only and free of chained commands. - SQL Injection Prevention: Parameterized query validation is implemented for direct SQL execution.
- Secure Credential Management: API keys and database passwords are loaded securely from the
.envfile.
π§ͺ Development & Testing
# Run unit tests
pytest tests/unit/
# Run integration tests
pytest tests/integration/
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.