Skip to main content
Glama
Maxim2324

MCP PostgreSQL Server

by Maxim2324

MCP PostgreSQL 服务器

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

  • 连接到 PostgreSQL 数据库

  • 将表模式公开为资源

  • 提供运行只读 SQL 查询的工具

  • 包括常见数据分析任务的提示

特征

  • 模式探索:浏览数据库模式、表和列

  • 只读查询执行:安全地对数据库运行 SQL 查询

  • 数据分析提示:针对常见分析任务的预建 SQL 模板

  • 数据可视化:生成可视化数据

  • 关系探索:可视化表关系和外键

  • API 文档:自动生成的 OpenAPI 规范

Related MCP server: PostgreSQL MCP Server

建筑学

此应用程序遵循模型-控制器-提供者(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

执照

麻省理工学院

Install Server
A
license - permissive license
B
quality
C
maintenance

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Maxim2324/mcp-server-test'

If you have feedback or need assistance with the MCP directory API, please join our Discord server