Multi-Database CRUD MCP Server

Multi-Database CRUD MCP Server

Exposes typed MCP tools for product CRUD operations, supporting SQLite, PostgreSQL, and planned MySQL.

Category
访问服务器

README

Multi-Database CRUD MCP Server

This learning project exposes typed MCP tools for product CRUD operations. SQLite works by default, PostgreSQL is supported through configuration, and MySQL can be added later without changing the MCP tools.

Architecture

User -> AI/MCP client -> MCP tools (server.py)
                         -> database adapter (database.py)
                            -> SQLite or PostgreSQL
                         <- structured result <- database

The AI never connects directly to the database or generates unrestricted SQL. It selects a narrow tool such as create_product, and the server validates the input before the database adapter executes parameterized SQL.

Available MCP tools

  • database_health
  • create_product
  • get_product
  • list_products
  • update_product
  • delete_product

Start with SQLite

SQLite is included with Python. It does not require Docker, a database server, username, or password.

  1. Install dependencies:

    uv sync
    
  2. Select SQLite in PowerShell:

    $env:DB_BACKEND="sqlite"
    $env:SQLITE_PATH="data/mcp_demo.db"
    
  3. Run the tests:

    uv run pytest
    
  4. Start MCP Inspector:

    uv run mcp dev src/mcp_database/server.py
    

The first database tool call automatically creates data/mcp_demo.db and its products table. Call database_health; it should return:

{"connected": true, "backend": "sqlite"}

Then try create_product, list_products, update_product, and delete_product in Inspector.

Connect an MCP client to SQLite

Use an absolute path for the project and SQLite file:

{
  "mcpServers": {
    "database-crud": {
      "command": "uv",
      "args": [
        "--directory",
        "C:\\absolute\\path\\to\\MCP_Database",
        "run",
        "mcp-database"
      ],
      "env": {
        "DB_BACKEND": "sqlite",
        "SQLITE_PATH": "C:\\absolute\\path\\to\\MCP_Database\\data\\mcp_demo.db"
      }
    }
  }
}

The selected database is controlled by DB_BACKEND. The older mcp-postgres command remains available as a compatibility alias.

Switch to PostgreSQL later

The MCP tools require no code changes. Start PostgreSQL and change the environment variables.

  1. Start the provided PostgreSQL container:

    docker compose up -d
    docker compose ps
    
  2. Select PostgreSQL:

    $env:DB_BACKEND="postgresql"
    $env:DATABASE_URL="postgresql://mcp_user:mcp_password@localhost:5432/mcp_demo"
    uv run mcp dev src/mcp_database/server.py
    

The container runs sql/init.sql on its first startup. SQLITE_PATH is ignored in PostgreSQL mode.

For an MCP client, replace its environment section with:

"env": {
  "DB_BACKEND": "postgresql",
  "DATABASE_URL": "postgresql://mcp_user:mcp_password@localhost:5432/mcp_demo"
}

MySQL in the future

MySQL is not implemented yet. DB_BACKEND=mysql deliberately returns a clear configuration error. To add it:

  1. Add PyMySQL or mysql-connector-python to pyproject.toml.
  2. Add a MySQL connection context manager in database.py.
  3. Implement the same six data functions using MySQL parameterized queries.
  4. Use MySQL schema syntax such as AUTO_INCREMENT.
  5. Add MySQL integration tests.

server.py will remain unchanged because the backend-specific code is isolated in database.py.

Important SQL differences

Concern SQLite PostgreSQL MySQL
Driver Built-in sqlite3 psycopg Future driver
Placeholder ? %s Usually %s
Generated ID AUTOINCREMENT IDENTITY AUTO_INCREMENT
Server needed No Yes Yes
Current status Implemented Implemented Planned

The adapter handles SQLite and PostgreSQL differences while presenting the same functions to the MCP server.

Project files

  • src/mcp_database/server.py: stable MCP contract and validation.
  • src/mcp_database/database.py: backend selection and database-specific SQL.
  • sql/init.sql: PostgreSQL schema and sample records.
  • compose.yaml: local PostgreSQL service.
  • tests/test_server.py: MCP tool unit tests.
  • tests/test_sqlite_database.py: real SQLite CRUD integration tests.
  • .env.example: configuration examples. A plain .env is not loaded automatically; supply variables through PowerShell or MCP client configuration.

How to explain it to a manager

“This proof of concept places a controlled MCP service between an AI assistant and a database. Instead of giving the model unrestricted SQL access, it exposes six typed and auditable operations. A database adapter lets developers use zero-setup SQLite locally and move to PostgreSQL later without changing the MCP contract.”

For production, add authentication, user-level authorization, secret management, audit logging, connection pooling, rate limiting, migrations, monitoring, backups, and separate read/write database roles.

Troubleshooting

  • DATABASE_URL is required for PostgreSQL: set it when DB_BACKEND=postgresql.
  • SQLite file missing: call any database tool once; it is created automatically.
  • PostgreSQL connection refused: verify docker compose ps reports a healthy container.
  • Inspector port 6274 is occupied: close the older Inspector terminal/session before starting another.
  • Tools do not appear: use an absolute project path and restart the MCP client.

This project uses the official MCP Python SDK.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选