Skip to main content

MCP Database Server with read-only access

Project description

MCP Database Server

A Model Context Protocol (MCP) server implementation in Python that provides strictly read-only access to databases. It allows Large Language Models (LLMs) to inspect schemas and query data safely without risking data modification.

Features

  • 🛡️ Strict Read-Only: Enforced at the driver level (e.g., SQLite ?mode=ro) or session level (MySQL SET SESSION TRANSACTION READ ONLY).
  • 🏗️ Repository Pattern: Abstracted database access allows for easy extension to other backends (PostgreSQL, MySQL) via Dependency Injection.
  • ⚡ FastMCP: Built efficiently using the official MCP Python SDK.
  • 🔌 Connection Pooling: Automatic connection pooling for MySQL using DBUtils to ensure performance and reliability.
  • 🔧 Easy Configuration: Managed via environment variables and Makefiles.

Project Structure

  • mcp_db/: Main Python package for the MCP server.
    • __init__.py: Entry point and server initialization.
    • db/: Repository implementations (sqlite_repository.py, mysql_repository.py).
  • seed.py: Utility script to populate the database (write access).
  • Makefile: Automation for common tasks.
  • pyproject.toml: Package configuration and dependencies.

Quick Start

Prerequisites

  • Python 3.12+ (managed via venv)
  • make (optional, but recommended)
  • MySQL Server (optional, if using MySQL backend)

1. Setup

Use the Makefile to create a virtual environment, install dependencies, and seed a test database:

make setup

This runs pip install -r requirements.txt and python seed.py.

2. Configuration

The project uses python-dotenv. Copy the example config and adjust as needed:

cp .env.example .env

Key Variables:

  • DB_ENGINE: sqlite (Default) or mysql
  • DB_ADDRESS: Path to db file (SQLite) or Host URL (MySQL).
  • DB_PORT: Database port (Default: 3306 for MySQL).
  • DB_USER / DB_PASSWORD: Database credentials (required for MySQL).
  • DB_SCHEMA: Database name (required for MySQL).

3. Run the Server

Start the MCP server:

make run

(Or directly via venv/bin/python server.py)

Usage with LLMs

Once running, the server exposes the following tools to connected MCP clients:

  1. list_tables()
    • Returns a list of all tables in the database.
  2. describe_table(table_name: str)
    • Returns the schema (columns, types, primary keys) for the specified table.
  3. read_query(query: str)
    • Executes a SQL SELECT query.
    • Note: Queries attempting to modify data will raise a Security Error or OperationalError.

Development

Running Tests

Execute the unit test suite:

make test

Extending

To add support for a new database (e.g., Postgres):

  1. Create db/postgres_repository.py implementation of DatabaseRepository.
  2. Update server.py to instantiate it when DB_ENGINE=postgres.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_db_server-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_db_server-0.1.2-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file mcp_db_server-0.1.2.tar.gz.

File metadata

  • Download URL: mcp_db_server-0.1.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mcp_db_server-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d0fabcba8f94de1ac6307243f0d60eb6e51918e7282b4aa54956c7f2aa77bc4c
MD5 79c7adb634b05b13ef9a282923c42996
BLAKE2b-256 c52fabd23ca16a379df9872c335228460cff3f03a4c4909da2560d7e69b0039c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_db_server-0.1.2.tar.gz:

Publisher: python-publish.yml on monkey-mode/mcp-db-server

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mcp_db_server-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mcp_db_server-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mcp_db_server-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 10849e857d485af4bbd3a264b4f0f2b0a82dafb5730181016a8352c805d57d43
MD5 2fcb17c956de3f8a9fcf502e1fac1887
BLAKE2b-256 f2b2d3fcc71c2489afa381fa8e11809dcff4ad685b9cfc579dfa7135039b2823

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_db_server-0.1.2-py3-none-any.whl:

Publisher: python-publish.yml on monkey-mode/mcp-db-server

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page