MCP Database Server

MCP Database Server

Enables AI assistants to interact with PostgreSQL databases through query execution and schema inspection, supporting multiple schemas for customer data, document management, loan systems, and asset leasing.

Category
访问服务器

README

MCP Database Server

A Model Context Protocol (MCP) server that provides PostgreSQL database access with query execution and schema inspection capabilities.

Overview

This MCP server enables AI assistants (like Claude) to interact with a PostgreSQL database containing data from multiple systems:

  • cs (CustomerScoreView): Customer and credit assessment data
  • dms (Data Management System): Document management data
  • los (Loan Management System): Loan and payment data
  • mls (Mahatheun Leasing System): Contract and asset leasing data

Features

  • Query Execution: Execute SQL SELECT, INSERT, UPDATE, DELETE queries
  • Schema Inspection: List schemas, tables, and column details
  • Multi-Schema Support: Organize data by system (cs, dms, los, mls)
  • Docker Setup: PostgreSQL in Docker with automatic initialization
  • CSV Import: Sample data included and ready to import

Prerequisites

  • Python 3.10 or higher
  • Docker and Docker Compose
  • pip (Python package installer)

Installation

  1. Clone or navigate to the project directory:

    cd c:\Users\chaya\project\mcp-database
    
  2. Install Python dependencies:

    pip install -r requirements.txt
    
  3. Configure environment variables:

    copy .env.example .env
    

    Edit .env if you need to change database credentials (optional).

Setup

1. Start PostgreSQL Database

Start the PostgreSQL container with Docker Compose:

docker-compose up -d

Verify the database is running:

docker-compose ps

2. Verify Database Initialization

The database will automatically initialize with schemas and sample data. Check the schemas:

docker-compose exec postgres psql -U postgres -d mcp_database -c "\dn"

List tables in a schema:

docker-compose exec postgres psql -U postgres -d mcp_database -c "\dt cs.*"

3. (Optional) Import CSV Data

Sample CSV files are provided in raw_data/. To import them into the database:

docker-compose exec postgres bash -c "cd /raw_data && find . -name '*.csv' -type f"

Import a specific CSV file:

docker-compose exec postgres psql -U postgres -d mcp_database -c "\COPY cs.customers FROM '/raw_data/cs/customers.csv' WITH CSV HEADER;"
docker-compose exec postgres psql -U postgres -d mcp_database -c "\COPY dms.documents FROM '/raw_data/dms/documents.csv' WITH CSV HEADER;"
docker-compose exec postgres psql -U postgres -d mcp_database -c "\COPY los.loans FROM '/raw_data/los/loans.csv' WITH CSV HEADER;"
docker-compose exec postgres psql -U postgres -d mcp_database -c "\COPY mls.contracts FROM '/raw_data/mls/contracts.csv' WITH CSV HEADER;"

Running the MCP Server

Test Locally

Run the server directly to test:

python src/server.py

The server will start and listen for MCP messages via stdio.

Configure with Claude Desktop

To use this MCP server with Claude Desktop, add it to your Claude configuration file:

Windows: %APPDATA%\Claude\claude_desktop_config.json

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

Add this configuration:

{
  "mcpServers": {
    "mcp-database": {
      "command": "python",
      "args": ["c:\\Users\\chaya\\project\\mcp-database\\src\\server.py"],
      "env": {
        "DB_HOST": "localhost",
        "DB_PORT": "5432",
        "DB_NAME": "mcp_database",
        "DB_USER": "postgres",
        "DB_PASSWORD": "postgres"
      }
    }
  }
}

Restart Claude Desktop to load the server.

Available MCP Tools

1. list_schemas

Lists all available database schemas.

Example:

Can you list all database schemas?

2. list_tables

Lists all tables, optionally filtered by schema.

Parameters:

  • schema (optional): Schema name to filter (e.g., "cs", "dms", "los", "mls")

Example:

Show me all tables in the cs schema

3. describe_table

Get detailed column information for a specific table.

Parameters:

  • table_name (required): Name of the table
  • schema (optional): Schema name (default: "public")

Example:

Describe the structure of the customers table in the cs schema

4. execute_query

Execute a SQL query on the database.

Parameters:

  • query (required): SQL query string
  • params (optional): Array of parameters for parameterized queries

Examples:

Query all customers with credit score above 700

SELECT * FROM cs.customers WHERE credit_score > 700

Get total loan amount by customer

SELECT customer_code, SUM(loan_amount) as total_loans 
FROM los.loans 
GROUP BY customer_code

Database Schema

cs (CustomerScoreView)

  • customers: Customer information and credit scores
  • credit_assessments: Credit assessment history

dms (Data Management System)

  • documents: Document metadata and storage info
  • document_versions: Document version history

los (Loan Management System)

  • loans: Loan accounts and terms
  • payments: Payment history

mls (Mahatheun Leasing System)

  • contracts: Lease contract information
  • assets: Asset details for leased items
  • lease_payments: Lease payment records

Project Structure

mcp-database/
├── src/
│   ├── server.py          # Main MCP server
│   ├── database.py        # Database connection manager
│   └── tools.py           # MCP tool definitions
├── db/
│   └── init.sql           # Database initialization script
├── raw_data/
│   ├── cs/                # CustomerScoreView CSV files
│   ├── dms/               # Data Management System CSV files
│   ├── los/               # Loan Management System CSV files
│   └── mls/               # Mahatheun Leasing System CSV files
├── docker-compose.yml     # Docker configuration
├── requirements.txt       # Python dependencies
├── pyproject.toml        # Python project metadata
└── .env.example          # Environment variable template

Troubleshooting

Docker container won't start

# Check logs
docker-compose logs postgres

# Restart container
docker-compose restart postgres

Connection refused error

  • Ensure PostgreSQL is running: docker-compose ps
  • Check port 5432 is not in use by another process
  • Verify .env file has correct credentials

Import errors in Python

# Reinstall dependencies
pip install --upgrade -r requirements.txt

Development

To extend the server:

  1. Add new tools in src/tools.py
  2. Register handlers in src/server.py
  3. Update database schema in db/init.sql
  4. Add sample data in raw_data/

License

This project is provided as-is for database integration purposes.

推荐服务器

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

官方
精选