home / mcp / mcp db python mcp server

MCP DB Python MCP Server

A lightweight Python implementation of the Model Context Protocol (MCP) server for AI and LLM integrations. | 轻量级 Python 实现的 Model Context Protocol (MCP) 服务端,用于 AI 与大语言模型集成。

Installation
Add the following to your MCP client configuration file.

Configuration

View docs
{
  "mcpServers": {
    "191341025-mcp-db-python": {
      "command": "python",
      "args": [
        "server.py"
      ],
      "env": {
        "DB_HOST": "localhost",
        "DB_NAME": "your_database",
        "DB_PASS": "your_password",
        "DB_PORT": "3306",
        "DB_TYPE": "mysql",
        "DB_USER": "your_user"
      }
    }
  }
}

You can run a Python-based MCP server that lets you safely inspect and read data from a MySQL source via JSON-RPC over standard input and output. This server focuses on exposing database schema metadata and read-only query results, making it a secure, easy-to-use tool for integrated workflows and large language model agents.

How to use

Connect to the server with an MCP client or your own script. You will be able to invoke a set of read-only operations to explore the MySQL schema, fetch table metadata, list databases and views, run safe read-only queries, and analyze execution plans. The available endpoints enable you to inspect tables, procedures, triggers, indexes, foreign keys, and more, all while enforcing strict read-only behavior to protect your production data.

How to install

Prerequisites: Install Python 3.10 or newer and ensure you can reach your MySQL instance.

Step-by-step commands to set up and run the MCP server locally:

git clone https://github.com/your-account/mcp-db-python.git
cd mcp-db-python

python -m venv venv
# Windows
.\venv\Scripts\activate
# macOS / Linux
source venv/bin/activate

pip install -r requirements.txt

# Configure database credentials in a .env file or environment before starting
# Example environment values are provided in the configuration section
```
```
# Start the MCP server (reads configuration from the environment or .env file)
python server.py

Configuration and environment

The server reads configuration from a .env file or runtime environment. You should specify the MySQL connection details so the server can surface schema metadata and perform read-only queries safely.

Notes and considerations

- The server is designed for safe, read-only access to the database in production environments. Ensure network security and access controls are correctly applied to limit who can issue read-only queries.

Available tools

listTables

List every table in the configured database.

getTableSchema

Fetch column metadata for a given table.

runQuery

Execute read-only SQL and return columns with rows.

getProcedureDefinition

Retrieve the CREATE statement of a stored procedure.

listDatabases

List accessible databases/schemas.

listViews

Enumerate views with definition snippets.

getTableStats

Return row counts and size statistics for tables.

getIndexInfo

Inspect indexes for columns, kinds, and uniqueness.

findForeignKeys

List foreign-key constraints and relationships.

getTriggers

Fetch trigger listings and definitions.

sampleRows

Sample a few rows from a table.

searchColumns

Search column names or comments by keyword.

describeColumn

Provide type, defaults, and constraint details for a column.

explainQuery

Run EXPLAIN on read-only SQL to inspect plans.

listProcedures

List stored procedures and functions.

listUsers

Summarize users and privileges where permitted.

getServerStatus

Return server status such as version and connections.

compareSchemas

Compare schema structures between databases/tables.

generateDDL

Generate full CREATE TABLE DDL.