mcp-server-bridge

nandeshkanagaraju/mcp-server-bridge

3.3

If you are the rightful owner of mcp-server-bridge 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.

The Universal Model Context Protocol (MCP) server is a secure database access layer that facilitates schema introspection and SQL execution via HTTP+SSE transport.

Tools
6
Resources
0
Prompts
0

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 SELECT statements 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.

2. 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

3. 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.

4. Initialize the Database

This script creates necessary tables and populates them with sample data in your local MySQL database.

python scripts/setup_database.py

5. 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

6. 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

EndpointMethodDescription
/api/v1/query/natural-languagePOSTConverts a natural language question to a SQL query and executes it.

Traditional MCP Tools

ToolDescription
execute_queryExecute validated SQL queries safely.
get_schemaRetrieve complete database schema.
list_tablesGet list of available tables.
describe_tableGet detailed table structure.
get_table_dataRetrieve paginated data from tables.
validate_queryValidate 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 uvicorn server is running.
  • Persistent Log File: The server also writes all logs to mcp_server.log in 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 .env file.

πŸ§ͺ Development & Testing

# Run unit tests
pytest tests/unit/

# Run integration tests
pytest tests/integration/