知识图谱管理服务器

一个高性能的知识图谱管理服务器,提供实体和关系的创建、读取、更新和删除功能,支持全文搜索和图遍历,适用于AI助手的记忆管理。

  • by okooo5km
  • Go
  • 本地部署
  • 94 星标
  • 更新于
在 GitHub 查看 okooo5km/memory-mcp-server-go

使用说明

内存MCP服务器(Go)

提供知识图管理功能的模型上下文协议服务器。该服务器使LLM能够在持久知识图中创建、读取、更新和删除实体和关系,帮助AI助手在对话中维护内存。这是官方的Go实现 TypeScript内存MCP服务器.

特性

  • 高性能存储:SQLite后端采用WAL模式,读/写连接分离以实现并发访问
  • 知识图谱管理:实体、关系和观察的持久图
  • 高级搜索:FTS5全文搜索,带BM25排名、同义词扩展和时间衰减评分
  • 图的遍历:搜索结果包括与1-hop相关的实体,以获得更丰富的上下文
  • 实体管理:合并重复实体、更新类型、修改观察值、检测冲突
  • 观测元数据:跟踪每个观察的来源、置信度和标签
  • MCP资源和提示:AI客户端可以被动加载图形摘要并使用引导式内存工作流
  • 柔性运输:支持stdio、SSE和流式HTTP,可选承载或OAuth 2.1身份验证
  • 无缝迁移:从JSONL自动升级到SQLite,无需干预
  • 交叉平台的:纯Go SQLite(无需CGO),适用于Linux、macOS和Windows

可用工具

核心CRUD

工具 说明
create_entities 使用名称、类型和观察值创建新实体
create_relations 创建实体之间的关系(主动语态)
add_observations 向现有实体添加观察结果
delete_entities 删除实体及其关联关系
delete_relations 删除特定关系
delete_observations 从实体中删除具体观察结果

查询

工具 说明
search_nodes 使用FTS5、同义词扩展和图遍历按关键字搜索实体。返回包含代码段和相关实体的轻量级结果
open_nodes 按确切名称获取特定实体的完整详细信息
read_graph 获取图表概览(summary 模式)或完全导出(full 模式)

实体管理

工具 说明
merge_entities 合并两个实体:将观测值和关系从源迁移到目标,然后删除源
update_entities 更改实体的类型
update_observations 替换观察内容
detect_conflicts 在实体的观察中发现潜在的重复和矛盾

MCP资源

URI 描述
memory://graph/summary 图形统计和实体类型分布
memory://graph/recent 最近访问的实体
memory://graph/types 所有实体和关系类型枚举
memory://entities/{name} 特定实体的完整详细信息

MCP提示

提示 描述
memory-recall 按主题回忆相关记忆
memory-save 分析对话文本并建议保存什么
memory-review 对实体的记忆进行全面审查

安装

自制(macOS/Linux)

brew install okooo5km/tap/mms

快速安装脚本(macOS/Linux)

curl -fsSL https://raw.githubusercontent.com/okooo5km/memory-mcp-server-go/main/scripts/install.sh | bash

选项: -v v0.2.3 对于特定版本, -d /usr/local/bin 用于自定义目录。

预构建二进制文件

下载自 GitHub 发布。适用于macOS(arm64/amd64)、Linux(arm64/AMD 64)和Windows(arm64/amd64)。

# Example: macOS Apple Silicon
curl -L https://github.com/okooo5km/memory-mcp-server-go/releases/latest/download/mms_VERSION_darwin_arm64.tar.gz | tar xz
chmod +x mms && mv mms ~/.local/bin/

从源代码构建

git clone https://github.com/okooo5km/memory-mcp-server-go.git
cd memory-mcp-server-go
make build        # binary in .build/

确保 ~/.local/bin (或您选择的目录)位于您的 PATH.

命令行参数

mms [options]
  -t, --transport string   Transport type: stdio, sse, or http (default "stdio")
  -m, --memory string      Memory file path (auto-detected if not specified)
  -p, --port int           Port for SSE/HTTP transport (default 8080)
  --host string            Host/IP to bind for SSE/HTTP transports (env: MMS_HOST, default all interfaces)
  -v, --version            Show version

  Storage:
  --storage string         Force storage type: sqlite or jsonl (auto-detected)
  --auto-migrate           Auto-migrate JSONL to SQLite (default true)

  Migration:
  --migrate string         Source JSONL file for manual migration
  --migrate-to string      Destination SQLite file
  --dry-run                Dry run migration
  --force                  Overwrite destination

  Streamable HTTP:
  --http-endpoint string   HTTP endpoint path (default "/mcp")
  --http-heartbeat string  Heartbeat interval (default "30s")
  --http-stateless         Stateless HTTP mode

  Auth:
  --auth-bearer string     Require Bearer token for SSE/HTTP

  OAuth 2.1 (mutually exclusive with --auth-bearer):
  --oauth-user string      OAuth login username (env: OAUTH_USER)
  --oauth-pass string      OAuth login password (env: OAUTH_PASS)
  --oauth-issuer string    OAuth issuer URL (auto-detect if empty, env: OAUTH_ISSUER)
  --oauth-store string     SQLite path for durable OAuth clients and tokens (env: OAUTH_STORE_PATH)
  --oauth-refresh-replay-grace string
                           Duration to accept replay of a rotated refresh token (env: OAUTH_REFRESH_REPLAY_GRACE, default 75m)

  CORS:
  --cors-origin string     Allowed CORS origins: '*' for all, or comma-separated list (default "*")

示例:

mms                                          # stdio, auto-detect storage
mms --memory /path/to/memory.json            # custom path, auto-migrates to SQLite
mms --transport sse --port 9000              # SSE transport
mms --transport http --auth-bearer mytoken   # Streamable HTTP with Bearer auth
mms --transport http --host 127.0.0.1 --oauth-user admin --oauth-pass secret --oauth-store /var/lib/mms/oauth.db  # OAuth 2.1 auth
mms --transport http --cors-origin "https://app.example.com,https://admin.example.com"  # CORS whitelist

配置

克劳德桌面/Claudel。

"mcpServers": {
  "memory": {
    "command": "mms",
    "env": {
      "MEMORY_FILE_PATH": "/path/to/memory.json"
    }
  }
}

克劳德桌面连接器(OAuth)

对于使用OAuth 2.1身份验证的远程部署,与Claude Desktop的自定义连接器兼容:

# Start server with OAuth
mms --transport http --host 127.0.0.1 --oauth-user admin --oauth-pass secret --oauth-store /var/lib/mms/oauth.db --port 8080

# Or with environment variables and explicit issuer
OAUTH_USER=admin OAUTH_PASS=secret OAUTH_STORE_PATH=/var/lib/mms/oauth.db \
  OAUTH_REFRESH_REPLAY_GRACE=24h \
  mms --transport http --oauth-issuer https://mcp.example.com

然后在Claude Desktop中: 设置→ 连接器→ 添加自定义连接器 → 输入您的服务器URL(例如。, https://mcp.example.com/mcp).Claude将自动发现OAuth端点并提示您登录。

光标

添加到光标设置>mcp.json:

{
  "mcpServers": {
    "memory": {
      "command": "mms",
      "env": {
        "MEMORY_FILE_PATH": "/path/to/memory.json"
      }
    }
  }
}

系统提示示例

You have access to a Knowledge Graph memory system that persists across conversations.

Saving memories:
- create_entities: Add new people, places, concepts (check search_nodes first to avoid duplicates)
- create_relations: Record how entities relate to each other
- add_observations: Add facts to existing entities

Retrieving memories:
- search_nodes: Find relevant entities by keyword (supports synonyms like JS→JavaScript)
- open_nodes: Get full details of specific entities
- read_graph: Get an overview of all stored knowledge (use "summary" mode first)

Managing memories:
- merge_entities: Combine duplicate entities
- detect_conflicts: Find contradictory observations
- update_entities / update_observations: Fix incorrect data

Always check your memory before answering questions that might require past context.

流式HTTP使用

Streamable HTTP端点接受JSON-RPC请求、通知和 响应。客户端响应,如对服务器的回复 ping 消息是 承认与 202 Accepted 根据MCP传输规范。

# 1. Initialize session
curl -i -X POST http://localhost:8080/mcp \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer mytoken' \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{}}}'

# 2. Listen for server messages
curl -N http://localhost:8080/mcp \
  -H 'Authorization: Bearer mytoken' \
  -H 'Mcp-Session-Id: <session-id>'

# 3. Call a tool
curl -s http://localhost:8080/mcp \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer mytoken' \
  -H 'Mcp-Session-Id: <session-id>' \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"search_nodes","arguments":{"query":"idea"}}}'

# 4. Terminate session
curl -X DELETE http://localhost:8080/mcp \
  -H 'Authorization: Bearer mytoken' \
  -H 'Mcp-Session-Id: <session-id>'

安全与部署

  • 部署在TLS(Nginx/Caddy/Traefik)后面,将服务器绑定到本地主机
  • 简单身份验证:使用 --auth-bearer $(openssl rand -hex 32) 面向程序化客户
  • OAuth 2.1:使用 --oauth-user/--oauth-pass 用于基于浏览器的登录(Claude Desktop Connectors)。集 --oauth-store 在生产环境中,注册的客户端和令牌可以在重启后继续运行,并使用限制性的umask(如systemd)运行服务 UMask=0077 以保护SQLite WAL/SHM文件。支持PKCE(S256)、动态客户端注册、持久令牌存储和刷新令牌轮换,并为并发或多工作客户端提供重播宽限窗口。使用 --oauth-refresh-replay-grace 适用于在多个进程中保留过时刷新令牌的客户端
  • 转发 Authorization 从反向代理到后端的标头;集 --oauth-issuer 在代理后面时指向公共URL
  • 以非root身份运行,仅打开所需端口,为不受信任的客户端启用速率限制

存储系统

存储类型

SQLite(推荐) JSONL(传统)
阅读/搜索 速度提高1.9倍 基线
记忆 效率提高1.9倍 基线
文件大小 更大 小3倍
初创公司 较慢 快55倍
特性 FTS5、ACID、WAL、并发读取 人类可读
最适合 >100个实体 <50个实体

迁移

# Automatic (default): just use your existing JSONL path
mms --memory /path/to/memory.json  # auto-migrates to .db

# Manual migration
mms --migrate /path/to/memory.json --migrate-to /path/to/memory.db

# Dry run
mms --migrate /path/to/memory.json --dry-run

知识图谱结构

  • 实体:具有名称、类型和观测值列表的节点(每个节点都有可选的元数据:源、置信度、标签)
  • 关系:主动语态中具有关系类型的实体之间的有向边
  • 观察:与实体相关的原子事实,支持基于访问模式的时间衰减排名

使用示例

创建实体

{
  "entities": [
    {
      "name": "John Smith",
      "entityType": "person",
      "observations": ["Software engineer", "Lives in San Francisco", "Enjoys hiking"]
    },
    {
      "name": "Acme Corp",
      "entityType": "company",
      "observations": ["Founded in 2010", "Tech startup"]
    }
  ]
}

建立关系

{
  "relations": [
    { "from": "John Smith", "to": "Acme Corp", "relationType": "works at" }
  ]
}

使用图遍历进行搜索

搜索“John”返回:

  • 直接命中:将“John”与观察片段匹配的实体
  • 相关实体:通过关系与“John”关联的实体(例如,“Acme Corp”通过“works at”关联)

合并重复实体

{
  "sourceName": "React.js",
  "targetName": "React"
}

将“React.js”中的所有观察结果和关系合并到“React”中,然后删除“React.js”。

检测冲突

{
  "entityName": "John Smith"
}

返回潜在的重复项(>60%前缀重叠)和矛盾项(反义词关键字对,如“喜欢/不喜欢”)。

发展

make fmt          # Format code
make check        # Static analysis (gofmt + go vet)
go test ./...     # Run tests
make build        # Build binary

# Full verification
make fmt && make check && go test ./... && make build

需求

  • 转到1.24+
  • github/mark3labs/mcp-go v0.19.0+
  • modernc.org/sqlite(纯Go sqlite,无CGO)

版本控制

版本通过以下方式管理 VERSION 文件。覆盖 make build VERSION=1.2.3 或 go build -ldflags "-X main.version=1.2.3".

许可证

MIT许可证。看 许可证 了解详情。