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/
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。