General-Purpose MCP Database Server

General-Purpose MCP Database Server

Provides LLMs with full PostgreSQL database access, including tools for query execution, schema management, and data export. It also features a dedicated insights system for storing business memos and supports both local stdio and remote HTTP transport.

Category
访问服务器

README

General-Purpose MCP Database Server (Python)

A Model Context Protocol (MCP) server that provides LLMs with generic database access capabilities for PostgreSQL databases. Built with Python and FastMCP.

Features

  • Query Tools: Execute SELECT queries, write operations (INSERT/UPDATE/DELETE), and export data
  • Schema Management: Create, alter, and drop tables, list tables, describe table schemas
  • Insights: Store and retrieve business insights in a dedicated memo table
  • Connection Pooling: Efficient database connection management
  • Docker Support: Easy containerized deployment
  • Flexible Configuration: Support for DATABASE_URL or individual connection parameters
  • Multiple Transport Modes: stdio (local) or SSE (HTTP for remote deployment)

Installation

Local Development

  1. Clone or navigate to the repository:
cd general-database-mcp-python
  1. Create a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure environment variables:
cp .env.example .env
# Edit .env with your database credentials
  1. Run the server:
python main.py

Docker Deployment

  1. Build the Docker image:
docker build -t general-database-mcp .
  1. Run the container:
docker run -p 8008:8008 \
  -e DATABASE_URL="postgresql://user:password@host:5432/database" \
  -e MCP_TRANSPORT=http \
  general-database-mcp

Configuration

Environment Variables

Database Configuration (choose one option):

Option 1: DATABASE_URL (recommended)

DATABASE_URL=postgresql://user:password@host:5432/database

Option 2: Individual parameters

DB_HOST=localhost
DB_PORT=5432
DB_NAME=your_database
DB_USER=your_username
DB_PASSWORD=your_password

Server Configuration:

PORT=8008
MCP_TRANSPORT=stdio  # Options: stdio (local) or http (remote)
API_KEY=your_secret_key  # Optional: for authentication

Available Tools

Query Tools

read_query

Execute SELECT queries to read data from the database.

  • Parameters: query (string) - SQL SELECT statement
  • Returns: Query results as JSON

write_query

Execute INSERT, UPDATE, or DELETE queries to modify data.

  • Parameters: query (string) - SQL modification statement
  • Returns: Number of affected rows

export_query

Execute a SELECT query and export results in CSV or JSON format.

  • Parameters:
    • query (string) - SQL SELECT statement
    • format (string) - "csv" or "json" (default: "json")
  • Returns: Formatted query results

Schema Management Tools

create_table

Create new tables in the database.

  • Parameters: query (string) - CREATE TABLE statement
  • Returns: Success status

alter_table

Modify existing table schema (add columns, rename, etc.).

  • Parameters: query (string) - ALTER TABLE statement
  • Returns: Success status

drop_table

Remove a table from the database with safety confirmation.

  • Parameters:
    • table_name (string) - Name of table to drop
    • confirm (boolean) - Must be True to proceed
  • Returns: Success status

list_tables

Get a list of all tables in the database.

  • Parameters: None
  • Returns: Array of table names

describe_table

View schema information for a specific table.

  • Parameters: table_name (string) - Name of table
  • Returns: Column definitions with types, constraints, etc.

Insights Tools

append_insight

Add a business insight to the memo table.

  • Parameters: insight (string) - Text of the insight
  • Returns: Success status

list_insights

List all business insights stored in the memo table.

  • Parameters: None
  • Returns: Array of insights with timestamps

Monitoring

health_check

Health check endpoint for monitoring and deployment verification.

  • Parameters: None
  • Returns: Server status and database connection info

Usage Examples

Using with Claude Desktop

Add to your Claude Desktop configuration file:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json Linux: ~/.config/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "database": {
      "command": "python",
      "args": [
        "/absolute/path/to/general-database-mcp-python/main.py"
      ],
      "env": {
        "DATABASE_URL": "postgresql://user:password@localhost:5432/mydb"
      }
    }
  }
}

Example Prompts for Claude

Query data:

Show me all tables in the database.
Describe the structure of the users table.
Select all records from the products table where price > 100.

Modify data:

Insert a new user with name 'John Doe' and email 'john@example.com'.
Update all products in the 'Electronics' category to increase price by 10%.
Delete all orders older than 2 years.

Schema operations:

Create a new table called 'logs' with columns for id, timestamp, and message.
Add a 'created_at' column to the users table.
Drop the temporary_data table (confirm=True).

Export data:

Export all customer data as CSV.
Export sales summary as JSON.

Business insights:

Add an insight: "Sales increased 25% in Q4 due to holiday promotions"
Show me all stored insights.

Architecture

general-database-mcp-python/
├── main.py                    # FastMCP server entry point
├── db/
│   ├── __init__.py
│   └── postgres_adapter.py   # PostgreSQL adapter with connection pooling
├── tools/
│   ├── __init__.py
│   ├── query_tools.py        # Query execution tools
│   ├── schema_tools.py       # DDL operation tools
│   └── insights_tools.py     # Business insights tools
└── utils/
    ├── __init__.py
    └── format_utils.py       # Response formatting and CSV conversion

Development

Running Tests

# TODO: Add test suite
python -m pytest tests/

Security Considerations

  1. Never expose database credentials in your conversations with Claude
  2. Use environment variables for sensitive configuration
  3. Enable API_KEY for production deployments
  4. Review SQL queries generated by the LLM before execution in production
  5. Use read-only database users when only SELECT access is needed

License

MIT License - See LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues, questions, or contributions, please open an issue on the repository.

Version

Current Version: 1.0.0

Acknowledgments

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选