PostgreSQL MCP服务器

一个生产级、可扩展的PostgreSQL模型上下文协议(MCP)服务器,专为VSCode、Cursor和其他IDE中的LLM编码代理设计。

  • by tmustafiz
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 tmustafiz/mcp-server-postgresql

使用说明

MCP PostgreSQL服务器

PostgreSQL的生产级可扩展模型上下文协议(MCP)服务器,专为VSCode、Cursor和其他IDE中的LLM编码代理而设计。

特性

  • 符合MCP标准:使用以下方式实现MCP规范 @modelcontextprotocol/sdk
  • PostgreSQL:用途 pg 设置连接池
  • 模块化工具:所有工具都组织在单独的模块中,以实现可扩展性
  • 运输:支持HTTP(流式HTTP)和STDIO
  • TypeScript:完全打字、严格和有记录
  • .env配置:用途 dotenv 用于配置
  • SSL支持:可配置的PostgreSQL SSL连接
  • 可扩展:易于添加自定义工具和扩展功能
  • 查询安全:针对资源密集型查询的内置保护

安装

npm install mcp-server-postgresql

用法

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { registerAllTools } from "mcp-server-postgresql/tools";

const server = new McpServer({
  name: "My MCP Server",
  version: "1.0.0"
});

// Register all built-in tools
registerAllTools(server);

// Start the server
server.start();

使用自定义工具进行扩展

您可以通过实现以下功能来创建自己的工具 ToolModule 接口:

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";

// Define your tool handler
const myCustomToolHandler: ToolCallback<{ param1: z.ZodString }> = async (args, extra) => {
  // Your tool implementation
  return { content: [{ type: "text", text: "Result" }] };
};

// Create a tool module
export function register(server: McpServer) {
  server.registerTool("my_custom_tool", {
    description: "Description of my custom tool",
    inputSchema: {
      param1: z.string()
    },
    outputSchema: {
      result: z.string()
    }
  }, myCustomToolHandler);
}

// Use in your server
import { register as registerCustomTool } from "./my-custom-tool.js";

const server = new McpServer({
  name: "My MCP Server",
  version: "1.0.0"
});

// Register built-in tools
registerAllTools(server);

// Register your custom tool
registerCustomTool(server);

server.start();

内置工具

  • list_tables:列出架构中的所有表
  • list_columns:列出表的所有列(带注释)
  • find_related_tables:显示表的直接FK关系
  • describe_relationship:描述两个表之间的关系
  • generate_erd_mermaid:为模式生成Mermaid ERD图
  • generate_erd_json:生成模式结构的JSON图
  • fuzzy_column_match:按自然语言短语模糊匹配列
  • sample_column_data:从列中返回示例数据
  • run_query:执行带有安全措施的SELECT查询:
    • 查询复杂性分析
    • 速率限制
    • 结果大小限制
    • 查询超时保护
    • 结构化响应

配置

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

# PostgreSQL Connection
PGHOST=localhost
PGPORT=5432
PGUSER=postgres
PGPASSWORD=yourpassword
PGDATABASE=mydb

# SSL Configuration (optional)
PGSSL=true                    # Enable SSL connection
PGSSL_REJECT_UNAUTHORIZED=true # Reject unauthorized SSL certificates

# Server Configuration
PORT=8080

发展

  1. 克隆仓库
  2. 安装依赖项
    npm install
    
  3. 构建
    npm run build
    
  4. 跑
    npm start
    

开发脚本

  • npm run build -构建TypeScript项目
  • npm start -运行内置服务器
  • npm run dev -使用tsx在开发模式下运行服务器
  • npm run release:beta -创建并发布测试版
  • npm run release:patch -创建并发布补丁版本
  • npm run release:minor -创建并发布次要版本
  • npm run release:major -创建并发布主要版本

依赖项

  • @modelcontextprotocol/sdk: ^1.12.1
  • dotenv: ^16.4.5
  • pg: ^8.12.0
  • string-similarity: ^4.0.4

许可证

麻省理工学院