MCP PostgreSQL Server

by Maxim2324
Verified

remote-capable server

The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.

Integrations

  • Manages configuration through environment variables using .env files for storing database credentials, server settings, and security parameters.

  • Supports repository cloning for installation and source code management during development.

  • Provides example code for API interaction, allowing clients to execute queries and interact with analysis templates.

MCP PostgreSQL 服务器

模型-控制器-提供程序 (MCP) 服务器:

  • 连接到 PostgreSQL 数据库
  • 将表模式公开为资源
  • 提供运行只读 SQL 查询的工具
  • 包括常见数据分析任务的提示

特征

  • 模式探索:浏览数据库模式、表和列
  • 只读查询执行:安全地对数据库运行 SQL 查询
  • 数据分析提示:针对常见分析任务的预建 SQL 模板
  • 数据可视化:生成可视化数据
  • 关系探索:可视化表关系和外键
  • API 文档:自动生成的 OpenAPI 规范

建筑学

此应用程序遵循模型-控制器-提供者(MCP)模式:

  • 模型层:直接与数据库交互
  • 提供层:业务逻辑和数据处理
  • 控制器层:API 端点和请求处理

安全功能

  • 只读查询验证
  • SQL注入保护
  • 速率限制
  • 参数化查询
  • 身份验证支持
  • CORS 配置

安装

  1. 克隆存储库:
    git clone <repository-url> cd mcp-postgres-server
  2. 安装依赖项:
    npm install
  3. 根据.env.template创建.env文件:
    cp .env.template .env
  4. 使用您的 PostgreSQL 数据库凭证更新.env文件。
  5. 启动服务器:
    npm start

配置

所有配置都通过环境变量进行管理:

  • 服务器:端口、环境、CORS 设置
  • 数据库:连接详细信息、池设置
  • 安全性:JWT 设置、速率限制
  • 查询:执行限制、结果大小限制

API 端点

架构端点

  • GET /api/schemas - 列出所有架构
  • GET /api/schemas/:schema/tables - 列出模式中的表
  • GET /api/schemas/:schema/tables/:table - 获取表架构详细信息
  • GET /api/schemas/:schema/relationships - 获取表关系
  • GET /api/structure - 获取完整的数据库结构
  • GET /api/search?q=term - 搜索表和列

查询端点

  • POST /api/query - 执行 SQL 查询
  • POST /api/query/explain - 获取查询执行计划
  • GET /api/schemas/:schema/tables/:table/sample - 获取示例数据
  • GET /api/schemas/:schema/tables/:table/stats - 获取表统计信息

分析提示端点

  • GET /api/prompts - 列出分析提示模板
  • GET /api/prompts/:templateId - 获取提示模板详细信息
  • POST /api/prompts/:templateId/generate - 从模板生成 SQL
  • GET /api/schemas/:schema/tables/:table/analysis/suggest - 获取分析建议

示例查询

基本表查询

// API request fetch('/api/query', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ sql: 'SELECT * FROM users LIMIT 10' }) }) .then(response => response.json()) .then(data => console.log(data));

使用分析提示

// Get suggested analysis for a table fetch('/api/schemas/public/tables/orders/analysis/suggest') .then(response => response.json()) .then(suggestions => { // Use a suggestion const suggestionId = suggestions.data[0].templateId; const params = suggestions.data[0].params; // Generate SQL from the template return fetch(`/api/prompts/${suggestionId}/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ params }) }); }) .then(response => response.json()) .then(data => { // Execute the generated SQL return fetch('/api/query', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ sql: data.data.sql }) }); }) .then(response => response.json()) .then(results => console.log(results));

发展

  • 以开发模式运行: npm run dev
  • 运行测试: npm test
  • Lint 代码: npm run lint

执照

麻省理工学院

You must be authenticated.

A
security – no known vulnerabilities
A
license - permissive license
A
quality - confirmed to work

连接到 PostgreSQL 数据库并提供用于安全探索模式、运行只读 SQL 查询以及使用预建模板执行数据分析的工具的服务器。

  1. Current Implementations
    1. 1. MCP PostgreSQL Server (mcp-psql/)
    2. 2. MCP Figma Server (mcp-figma/)
  2. Purpose
    1. Getting Started
      1. Contributing
        1. License
          ID: vaztx3bu73