PostgreSQL语义搜索服务器

一个为PostgreSQL提供语义和全文搜索功能的MCP服务器,专为LLM内存管理设计,支持多租户集合管理和简化接口。

  • by blevinstein
  • JavaScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 blevinstein/simple-postgres-mcp

使用说明

简单的PostgreSQL MCP

PostgreSQL的模型上下文协议(MCP)服务器 pg向量 它为LLM内存管理提供语义和全文搜索功能。

为什么选择MCP服务器?

主要用例:多租户集合管理

  • 多个MCP服务器可以使用具有隔离存储的同一PostgreSQL实例
  • 每个服务器实例可以在不同的表上操作(例如,每个用户、每个帐户、每个应用程序)
  • 实现经济高效的共享基础架构,同时保持数据隔离
  • 非常适合每个客户都需要自己的矢量空间的SaaS应用程序

次要好处:代理友好的简化界面

  • 代理人和LLM可能会遇到太多的工具选择,导致决策失误
  • 此服务器仅提供3个重点工具(store_memory, search_memory, forget_memory)
  • 简化的界面使其更容易与其他MCP服务器和功能结合
  • 针对内存/知识管理工作流程而不是完整的数据库管理进行了优化

特性

  • 语义搜索:使用具有L2距离的pgvector进行基于向量的相似性搜索
  • 全文搜索:PostgreSQL原生tsvector/tsquery全文搜索
  • 内存管理:使用自动生成的ID存储、搜索和删除文档/内存
  • 灵活的嵌入模型:支持OpenAI、Vertex AI和谷歌嵌入模型
  • 自动架构管理:使用适当的索引自动创建表
  • 可配置集合:使用默认表或按操作指定
  • JSONB元数据:通过原生PostgreSQL JSONB提供丰富的元数据支持

先决条件

  • PostgreSQL:安装了pgvector扩展的PostgreSQL 12+
  • API密钥:嵌入模型所需的环境变量:
    # For Google models (default)
    export GEMINI_API_KEY="your_google_api_key"
    
    # For OpenAI models
    export OPENAI_API_KEY="your_openai_api_key"
    
    # For Vertex AI models
    export VERTEX_PROJECT_ID="your-gcp-project"
    export VERTEX_LOCATION="us-central1"
    export VERTEX_CREDENTIALS='{"type":"service_account","project_id":"your-project","private_key":"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n","client_email":"...@...iam.gserviceaccount.com",...}'
    

安装

# Install dependencies
pnpm install

# Build the package
pnpm run build

# Test the installation
pnpm run test

Docker快速入门

最简单的入门方法是使用Docker Compose:

# Start PostgreSQL with pgvector
pnpm run docker:up

# View logs
pnpm run docker:logs

# Stop PostgreSQL
pnpm run docker:down

这将在端口5432上启动PostgreSQL:

  • 数据库: mcp_memories
  • 用户: postgres
  • 密码: postgres
  • 预安装pgvector扩展

用法

运行MCP服务器

# Using npx (after building)
npx simple-postgres-mcp --host localhost --port 5432 --database mcp_memories

# With a default collection/table
npx simple-postgres-mcp --collection my_memories

# Using OpenAI embeddings
npx simple-postgres-mcp --embedding-model openai/text-embedding-3-small

# Development mode (without building)
pnpm run dev --help

命令行选项

  • --host:PostgreSQL服务器主机(默认值: localhost)
  • --port:PostgreSQL服务器端口(默认值: 5432)
  • --database:PostgreSQL数据库名称(默认值: mcp_memories)
  • --collection:默认表名(可选-根据需要创建表)
  • --embedding-model:嵌入要使用的模型(默认值: google/text-embedding-004)

环境变量

  • PGUSER:PostgreSQL用户名(默认值: postgres)
  • PGPASSWORD:PostgreSQL密码(默认值: postgres)

可用工具

1. store_memory

在Postgres中存储文档/内存,并自动生成嵌入和ID。

参数:

  • content (string,必填):要存储的文本内容
  • metadata (object,可选):要与内存一起存储的其他元数据
  • collection (字符串,可选):集合名称(如果未设置为默认值)

答复:

{
  "success": true,
  "operation": "store",
  "result": {
    "id": "mem_1234567890_abc123def",
    "collection": "my_memories",
    "content_length": 42,
    "embedding_dimensions": 768,
    "embedding_model": "google/text-embedding-004",
    "metadata": {"topic": "AI"},
    "created_at": "2024-01-01T12:00:00.000Z"
  }
}

2. search_memory

使用语义或全文搜索搜索内存/文档。

参数:

  • query (字符串,必填):搜索查询文本
  • mode (字符串,可选):搜索模式- semantic 或 fulltext (默认值: semantic)
  • limit (数字,可选):最大结果数(默认值:10)
  • collection (字符串,可选):集合名称(如果未设置为默认值)

答复:

{
  "success": true,
  "operation": "search",
  "result": {
    "query": "machine learning",
    "mode": "semantic",
    "count": 2,
    "memories": [
      {
        "id": "mem_1234567890_abc123def",
        "content": "Machine learning is a subset of AI...",
        "similarity": 0.92,
        "metadata": {"topic": "AI"},
        "created_at": "2024-01-01T12:00:00.000Z"
      }
    ]
  }
}

3. forget_memory

使用PostgreSQL自动生成的ID从PostgreSQL中删除内存/文档。

参数:

  • id (字符串,必填):自动生成要删除的内存ID(格式: mem_timestamp_randomstring)
  • collection (字符串,可选):表名(如果未设置为默认值)

答复:

{
  "success": true,
  "operation": "delete",
  "result": {
    "id": "mem_1234567890_abc123def",
    "collection": "my_memories"
  }
}

使用Claude Desktop进行配置

添加到您的Claude Desktop配置文件中:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "postgres-memories": {
      "command": "npx",
      "args": [
        "simple-postgres-mcp",
        "--host", "localhost",
        "--port", "5432",
        "--database", "mcp_memories",
        "--collection", "my_memories"
      ],
      "env": {
        "GEMINI_API_KEY": "your-api-key-here",
        "PGUSER": "postgres",
        "PGPASSWORD": "postgres"
      }
    }
  }
}

建筑

数据库模式

每个集合/表都是使用以下模式创建的:

CREATE TABLE {collection_name} (
  id TEXT PRIMARY KEY,
  content TEXT NOT NULL,
  embedding vector(768),  -- pgvector, dimension varies by model
  content_fts tsvector GENERATED ALWAYS AS (to_tsvector('english', content)) STORED,
  metadata_json JSONB,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Indexes for performance
CREATE INDEX {table}_content_fts_idx ON {table} USING GIN(content_fts);
CREATE INDEX {table}_embedding_idx ON {table} USING hnsw (embedding vector_l2_ops);

搜索方法

  • 语义搜索:使用pgvector的L2距离运算符(<->)为了相似性
  • 全文搜索:使用PostgreSQL的原生tsvector/tsquery和ts_rank进行相关性分析

许可证

麻省理工学院