PostgreSQL Query MCP Server

Integrations

  • Runs as a Node.js application, serving as the execution environment for the MCP server

  • Provides a secure read-only interface to PostgreSQL databases, allowing execution of validated SELECT queries with timeout protection

MCP Postgres 查询服务器

模型上下文协议 (MCP) 服务器实现,用于以只读模式查询 PostgreSQL 数据库,旨在与 Claude Desktop 和其他 MCP 客户端配合使用。

概述

该项目实现了模型上下文协议(MCP)服务器,它提供:

  1. PostgreSQL 数据库的安全只读接口
  2. 通过 MCP 协议与 Claude Desktop 集成
  3. SQL 查询验证以确保仅执行 SELECT 查询
  4. 查询超时保护(10秒)

先决条件

  • Node.js(v14 或更高版本)
  • npm(Node.js 附带)
  • PostgreSQL 数据库(通过命令行提供连接详细信息)

安装

# Clone the repository git clone https://github.com/RathodDarshil/mcp-postgres-query-server.git cd mcp-postgres-query-server # Install dependencies npm install # Build the project npm run build

连接到 Claude Desktop

您可以配置 Claude Desktop 自动启动并连接到 MCP 服务器:

  1. 访问 Claude Desktop 配置文件:
    • 打开 Claude 桌面
    • 前往“设置”>“开发者”>“编辑配置”
    • 这将在您的默认文本编辑器中打开配置文件
  2. 将 postgres-query-server 添加到claude_desktop_config.jsonmcpServers部分:
{ "mcpServers": { "postgres-query": { "command": "node", "args": [ "/path/to/your/mcp-postgres-query-server/dist/index.js", "postgresql://username:password@hostname:port/database" ] } } }
  1. /path/to/your/替换为项目目录的实际路径。
  2. 将 PostgreSQL 连接字符串替换为您的实际数据库凭据。
  3. 保存文件并重启 Claude Desktop。MCP 服务器现在应该出现在“设置”中的 MCP 服务器选择下拉菜单中。

示例配置

以下是 postgres-query 配置文件的完整示例:

{ "mcpServers": { "postgres-query": { "command": "node", "args": [ "/Users/darshilrathod/mcp-servers/mcp-postgres-query-server/dist/index.js", "postgresql://user:password@localhost:5432/mydatabase" ] } } }

更新配置

要更新您的 Claude Desktop 配置:

  1. 打开 Claude 桌面
  2. 前往“设置”>“开发者”>“编辑配置”
  3. 对配置文件进行更改
  4. 保存文件
  5. 重新启动 Claude Desktop 以使更改生效
  6. 如果您已更新 MCP 服务器代码,请确保在重新启动之前使用npm run build重新构建它

特征

  • 只读数据库访问:出于安全考虑,只允许执行 SELECT 查询
  • 查询验证:防止潜在的有害 SQL 操作
  • 超时保护:运行时间超过 10 秒的查询将自动终止
  • MCP 协议支持:完整实现模型上下文协议
  • JSON 响应格式:查询结果以结构化的 JSON 格式返回

API

工具

查询-postgres

对配置的 PostgreSQL 数据库执行只读 SQL 查询。

参数:

  • query (字符串):要执行的 SQL SELECT 查询

回复:

  • JSON 对象包含:
    • rows :结果集行
    • rowCount :返回的行数
    • fields :列元数据

例子:

query-postgres: SELECT * FROM users LIMIT 5

发展

主服务器实现位于src/index.ts中。关键组件:

  • PostgreSQL 连接池设置
  • 查询验证逻辑
  • MCP 服务器配置
  • 工具和资源定义

要修改服务器的行为,您可以:

  • 编辑isReadOnlyQuery()中的查询验证逻辑
  • 向 MCP 服务器添加额外的工具或资源
  • 修改查询超时时长(目前为10秒)

安全注意事项

  • 服务器验证所有查询以确保它们是只读的
  • 使用 SSL 连接数据库
  • 查询超时可防止资源耗尽
  • 不允许进行任何写入操作
  • 数据库凭证直接通过命令行参数传递,而不是存储在文件中

执照

国际学习中心

贡献

欢迎贡献代码!欢迎提交 Pull 请求。

-
security - not tested
F
license - not found
-
quality - not tested

安全的模型上下文协议服务器允许 Claude 对 PostgreSQL 数据库执行只读 SQL 查询,从而实现与数据库数据的自然语言交互。

  1. Overview
    1. Prerequisites
      1. Installation
        1. Connecting to Claude Desktop
          1. Example Configuration
          2. Updating Configuration
        2. Features
          1. API
            1. Tools
          2. Development
            1. Security Considerations
              1. License
                1. Contributing
                  ID: uo7iwjby9f