SQL数据库连接服务

一个连接Claude到PostgreSQL数据库的MCP服务器,提供原始SQL访问和预构建的分析工具,用于制造数据洞察。

  • by bhaiyasingh45
  • 本地部署
  • 更新于
在 GitHub 查看 bhaiyahnsingh45/SQL-MCP-Server

使用说明

SQL MCP服务器

一个MCP(模型上下文协议)服务器,将Claude连接到PostgreSQL数据库,为制造数据洞察提供原始SQL访问和预构建的分析工具。

内置于 FastMCP 设计用于与 克劳德代码.

先决条件

  • Python 3.10+
  • 紫外线 (推荐)或pip
  • PostgreSQL数据库在本地运行(或可远程访问)

设置

1.克隆存储库

git clone https://github.com/your-username/SQL-MCP-Server.git
cd SQL-MCP-Server

2.安装依赖项

使用紫外线(推荐):

uv sync

上面写着 pyproject.toml 并自动创建具有所有依赖关系的虚拟环境。

使用pip(替代):

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

pip install -r requirements.txt

3.配置数据库连接

服务器从环境变量中读取数据库凭据。复制示例文件并填写您的值:

cp .env.example .env

然后编辑 .env:

DB_HOST=localhost
DB_PORT=5432
DB_NAME=bhaiya_and_company
DB_USER=postgres
DB_PASSWORD=your_password_here

所有变量都有合理的默认值,因此您只需要设置与您的设置不同的变量。

变量 默认值 描述
DB_HOST localhost PostgreSQL主机
DB_PORT 5432 PostgreSQL端口
DB_NAME bhaiya_and_company 数据库名称
DB_USER postgres 数据库用户
DB_PASSWORD root 数据库密码

注: 永不承诺 .env 版本控制。添加到 .gitignore.

4.在本地测试服务器

uv run fastmcp run main.py

如果服务器启动时没有错误,则已准备好连接到Claude Code。

连接到克劳德代码

选项A:项目级配置(推荐)

创建一个 .mcp.json 项目根目录中的文件:

{
  "mcpServers": {
    "sql_mcp": {
      "command": "uv",
      "args": [
        "--directory",
        "/absolute/path/to/SQL-MCP-Server",
        "run",
        "fastmcp",
        "run",
        "main.py"
      ],
      "env": {
        "PYTHONUNBUFFERED": "1",
        "DB_HOST": "localhost",
        "DB_PORT": "5432",
        "DB_NAME": "bhaiya_and_company",
        "DB_USER": "postgres",
        "DB_PASSWORD": "your_password_here"
      }
    }
  }
}

重要提示: 替换 /absolute/path/to/SQL-MCP-Server 在您的计算机上显示此项目的实际绝对路径。关于Windows的使用 \\ 作为路径分隔符(例如。 C:\\MyRepos\\SQL-MCP-Server).

这 env 挡住 .mcp.json 是您传递数据库凭据的地方。当Claude Code启动MCP服务器时,这些被设置为环境变量。

选项B:全局配置

要使服务器在所有Claude Code会话中可用,请将相同的配置添加到全局设置中:

claude mcp add sql_mcp -- uv --directory /absolute/path/to/SQL-MCP-Server run fastmcp run main.py

验证连接

配置后,在项目目录中启动Claude Code:

claude

然后让Claude列出可用的工具或运行查询:

> list all tables in the database
> show me the OEE report for Q1 2024

可用工具

核心工具

工具 说明 参数
test_sql_query 执行任何SQL查询并返回结果 query
get_table_schema 获取表的列详细信息 table_name
list_tables 列出数据库中的所有公用表 无

分析/洞察工具

工具 说明 参数
get_production_summary 生产、拒收、拒收率、能源和材料使用总量 start_date, end_date
get_oee_report 每台机器的OEE(可用性x性能x质量) start_date, end_date
get_sales_summary 按产品、地区、客户或月份划分的收入明细 start_date, end_date, group_by
get_inventory_alerts 关键或低于再订购级别的项目 无
get_downtime_analysis 按类别、机器或根本原因分组的停机事件 start_date, end_date, group_by
get_quality_report 质量评分和缺陷类型细分 start_date, end_date
get_operator_performance 按产量和废品率排名的操作员 start_date, end_date
get_maintenance_summary 每台机器的维护成本和频率 start_date, end_date
get_top_customers 收入最高的客户 limit, start_date, end_date
get_machine_status 当前机器状态和过去30天的生产统计数据 无

使用所有日期参数 YYYY-MM-DD 格式,默认为2024年全年。

数据库模式

该服务器专为具有13个表的制造数据库而设计:

customers          - Customer info (industry, location, type)
products           - Product catalog (PET bottles, glass bottles, etc.)
machines           - Machine registry (injection molding machines)
operators          - Operator profiles (experience, efficiency rating)
shifts             - Shift definitions (morning, afternoon, night)
materials          - Raw materials (PET resin, HDPE, glass, etc.)
suppliers          - Supplier details and ratings
productiondata     - Hourly production records (8,784 rows for 2024)
salesdata          - Sales transactions (820 orders for 2024)
qualitychecks      - Quality inspection results and defect types
downtimeevents     - Machine downtime events with root causes
maintenancerecords - Maintenance logs with costs and parts replaced
inventory          - Current stock levels and reorder alerts

克劳德提示示例

# Production
"Give me a production summary for March 2024"
"What's the total rejection rate for Q1?"

# OEE
"Show OEE for all machines in 2024"
"Which machine has the best OEE?"

# Sales
"Break down sales by region for 2024"
"Show monthly revenue trend"
"Who are the top 5 customers?"

# Operations
"Are there any inventory alerts?"
"What caused the most downtime this year?"
"Show me the quality report for last quarter"
"Rank operators by performance"
"How much did we spend on maintenance per machine?"
"What's the current status of all machines?"

项目结构

SQL-MCP-Server/
├── main.py              # MCP server with all tool definitions
├── pyproject.toml       # Project metadata and dependencies (uv)
├── requirements.txt     # Dependencies (pip)
├── uv.lock              # Lock file (auto-generated by uv)
├── .mcp.json            # Claude Code MCP configuration
├── .env.example         # Example environment variables
├── .python-version      # Python version pin (3.10)
└── README.md

许可证

麻省理工学院