MCP Memory Server - AI的持久化知识图谱记忆系统

MCP Memory Server - AI的持久化知识图谱记忆系统

官方实现 | Stars: 2000+ | TypeScript | MIT License

概述

MCP Memory Server 是 Model Context Protocol 官方提供的知识图谱持久化记忆系统,为 AI 助手提供跨会话的长期记忆能力。通过实体-关系-观察三层架构,它能够像人类一样存储和检索结构化的记忆信息,实现真正的上下文理解和个性化交互。

该服务器采用知识图谱设计理念,将信息组织为互相关联的实体网络。每个实体可以拥有类型、多个观察信息,并通过有向关系与其他实体连接。这种设计使得 AI 能够理解复杂的实体关系、追踪用户偏好、维护长期对话上下文,特别适合需要记忆功能的智能应用场景。

核心特性

  • 官方实现,Anthropic 官方维护和支持
  • 🧠 知识图谱架构,实体-关系-观察三层结构
  • 💾 持久化存储,跨会话保持记忆
  • 🔍 强大的查询能力,支持全图搜索和精确查询
  • 🎯 灵活的类型系统,支持自定义实体类型
  • 🔄 完整的 CRUD 操作,创建、读取、更新、删除
  • 🐳 多种部署方式,Docker 和 NPX 灵活选择
  • 📊 原子化数据管理,每条观察独立存储

工具列表

1. create_entities

功能:在知识图谱中创建新的实体节点

参数

  • entities (array, 必需) - 要创建的实体列表
    • name (string, 必需) - 实体的唯一名称(建议使用下划线分隔,如 John_Doe
    • entityType (string, 必需) - 实体类型(如 personorganizationconceptproject
    • observations (string[], 必需) - 关于该实体的离散观察信息列表

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"entities": [
{
"name": "John_Doe",
"entityType": "person",
"observations": [
"Works as a software engineer at Acme Corp",
"Lives in San Francisco",
"Interested in AI and machine learning",
"Prefers Python and TypeScript for development"
]
},
{
"name": "Acme_Corp",
"entityType": "organization",
"observations": [
"Technology company founded in 2010",
"Specializes in AI solutions",
"Located in Silicon Valley"
]
}
]
}

最佳实践

  • 实体名称应该唯一且描述性强
  • 每条观察应该是原子性的,只描述一个事实
  • 选择合适的实体类型便于后续分类和查询

2. create_relations

功能:在实体之间创建有向关系连接

参数

  • relations (array, 必需) - 要创建的关系列表
    • from (string, 必需) - 源实体名称
    • to (string, 必需) - 目标实体名称
    • relationType (string, 必需) - 关系类型(使用主动语态)

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"relations": [
{
"from": "John_Doe",
"to": "Acme_Corp",
"relationType": "works_at"
},
{
"from": "John_Doe",
"to": "Jane_Smith",
"relationType": "collaborates_with"
},
{
"from": "Jane_Smith",
"to": "AI_Project",
"relationType": "leads"
}
]
}

常用关系类型

  • works_at - 工作于
  • manages - 管理
  • collaborates_with - 协作
  • knows - 认识
  • owns - 拥有
  • belongs_to - 属于
  • leads - 领导
  • reports_to - 汇报给

3. add_observations

功能:向已存在的实体添加新的观察信息

参数

  • observations (array, 必需) - 观察数据列表
    • entityName (string, 必需) - 实体名称
    • contents (string[], 必需) - 新的观察信息内容列表

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"observations": [
{
"entityName": "John_Doe",
"contents": [
"Recently completed a project on distributed systems",
"Speaks English and Spanish fluently",
"Prefers remote work",
"Active contributor to open source projects"
]
}
]
}

使用场景

  • 用户透露新的个人信息
  • 项目状态更新
  • 偏好设置变更
  • 技能和经验积累

4. delete_entities

功能:从知识图谱中删除实体及其关联的所有关系

参数

  • entityNames (string[], 必需) - 要删除的实体名称列表

示例

1
2
3
{
"entityNames": ["Old_Project", "Inactive_User"]
}

注意事项

  • 删除实体会自动删除所有相关的关系
  • 操作不可逆,请谨慎使用
  • 建议先使用 open_nodes 确认实体信息

5. delete_observations

功能:从实体中删除特定的观察信息

参数

  • deletions (array, 必需) - 要删除的观察数据列表
    • entityName (string, 必需) - 实体名称
    • observations (string[], 必需) - 要删除的观察信息列表

示例

1
2
3
4
5
6
7
8
9
10
11
{
"deletions": [
{
"entityName": "John_Doe",
"observations": [
"Lives in New York",
"Interested in blockchain technology"
]
}
]
}

使用场景

  • 更正错误信息
  • 删除过时数据
  • 隐私信息清理

6. delete_relations

功能:删除实体之间的特定关系

参数

  • relations (array, 必需) - 要删除的关系列表
    • from (string, 必需) - 源实体名称
    • to (string, 必需) - 目标实体名称
    • relationType (string, 必需) - 关系类型

示例

1
2
3
4
5
6
7
8
9
{
"relations": [
{
"from": "John_Doe",
"to": "Old_Company",
"relationType": "works_at"
}
]
}

7. read_graph

功能:检索整个知识图谱的结构,包括所有实体、关系和观察信息

参数:无

返回:完整的知识图谱 JSON 对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"entities": [
{
"name": "John_Doe",
"entityType": "person",
"observations": ["..."]
}
],
"relations": [
{
"from": "John_Doe",
"to": "Acme_Corp",
"relationType": "works_at"
}
]
}

使用场景

  • 导出知识图谱
  • 全局分析
  • 数据备份
  • 可视化展示

8. search_nodes

功能:在知识图谱中搜索实体,支持按实体名称、类型或观察内容搜索

参数

  • query (string, 必需) - 搜索查询字符串

示例

1
2
3
{
"query": "software engineer"
}

搜索范围

  • 实体名称
  • 实体类型
  • 观察信息内容

返回:匹配查询的实体列表及其详细信息

9. open_nodes

功能:根据名称检索特定实体节点的详细信息

参数

  • names (string[], 必需) - 要检索的实体名称列表

示例

1
2
3
{
"names": ["John_Doe", "Acme_Corp", "AI_Project"]
}

返回:指定实体的完整详细信息,包括所有观察和关系

使用场景

  • 精确查询已知实体
  • 获取实体完整信息
  • 验证实体存在性

配置方式

环境变量

Memory Server 使用本地文件系统存储,无需额外的环境变量配置。数据默认存储在容器或本地目录中。

Claude Desktop 配置

~/Library/Application Support/Claude/claude_desktop_config.json 中添加:

1
2
3
4
5
6
7
8
{
"mcpServers": {
"memory": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-memory"]
}
}
}

VS Code (Claude Code) 配置

在项目根目录创建 .vscode/mcp.json

1
2
3
4
5
6
7
8
{
"mcpServers": {
"memory": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-memory"]
}
}
}

Docker 部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"mcpServers": {
"memory": {
"command": "docker",
"args": [
"run",
"-i",
"-v",
"claude-memory:/app/dist",
"--rm",
"mcp/memory"
]
}
}
}

Docker 数据持久化

1
2
3
4
5
6
7
8
# 创建命名卷
docker volume create claude-memory

# 运行容器
docker run -i -v claude-memory:/app/dist --rm mcp/memory

# 查看数据卷
docker volume inspect claude-memory

本地开发

1
2
3
4
5
6
7
8
9
10
11
12
# 克隆仓库
git clone https://github.com/modelcontextprotocol/servers.git
cd servers/src/memory

# 安装依赖
npm install

# 构建
npm run build

# 运行
npm start

使用场景

1. 个性化对话助手

构建能够记住用户偏好和历史的智能助手。

示例流程

  1. 用户首次交互,创建用户实体和偏好观察
  2. 记录用户的工作信息、兴趣爱好、沟通风格
  3. 后续对话中检索用户记忆,提供个性化响应
  4. 持续更新用户偏好和新信息

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 创建用户实体
create_entities({
entities: [{
name: "User_Alice",
entityType: "person",
observations: [
"Prefers concise technical explanations",
"Works in data engineering",
"Uses Python and SQL daily",
"Interested in data pipeline optimization"
]
}]
})

// 添加新观察
add_observations({
observations: [{
entityName: "User_Alice",
contents: [
"Recently started learning Apache Spark",
"Working on a real-time streaming project"
]
}]
})

2. 客户关系管理 (CRM)

追踪客户信息、互动历史和商业关系。

示例流程

  1. 创建客户实体和公司实体
  2. 建立客户-公司关系
  3. 记录每次沟通和项目进展
  4. 查询客户历史,提供针对性服务

知识图谱结构

1
2
3
4
5
6
Customer_Bob (person)
├─ observations: ["Decision maker", "Budget: $100K", "Needs delivered by Q4"]
└─ works_at → TechStart_Inc (organization)
└─ observations: ["Series A startup", "50 employees", "Focus on AI SaaS"]

Customer_Bob ─ interested_in → Product_Enterprise_Plan

3. 项目知识沉淀

维护项目文档、决策历史和团队知识。

示例流程

  1. 为每个项目创建实体
  2. 记录技术选型、架构决策、遇到的问题
  3. 建立项目成员关系
  4. 新成员加入时快速检索项目上下文

实体设计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"entities": [
{
"name": "Project_DataPipeline",
"entityType": "project",
"observations": [
"Tech stack: Python, Apache Airflow, PostgreSQL",
"Started: 2025-01-15",
"Team size: 5 engineers",
"Main challenge: handling 10TB+ daily data"
]
}
]
}

4. 学习路径追踪

记录用户的学习进度、已掌握技能和待学习内容。

示例流程

  1. 创建用户和技能实体
  2. 建立”已掌握”、”正在学习”关系
  3. 记录学习笔记和难点
  4. 根据进度推荐后续学习内容

5. 多轮对话增强

在复杂的多轮对话中维护上下文状态。

示例流程

  1. 对话开始时创建会话实体
  2. 记录讨论的主题、决策、待办事项
  3. 跨越多天的对话也能保持连续性
  4. 支持”上次我们聊到哪了”这类查询

技术架构

知识图谱三层结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
┌─────────────────────────────────────────────────────────┐
│ Knowledge Graph │
├─────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Entity 1 │ │ Entity 2 │ │
│ │ │ │ │ │
│ │ Type: person │─────────│ Type: org │ │
│ │ │ Relation│ │ │
│ │ Observations:│ │ Observations:│ │
│ │ - Obs 1 │ │ - Obs A │ │
│ │ - Obs 2 │ │ - Obs B │ │
│ └──────────────┘ └──────────────┘ │
│ │
└─────────────────────────────────────────────────────────┘

实体层(Entities)
├─ 唯一标识符(name)
├─ 实体类型(entityType)
└─ 观察列表(observations[])

关系层(Relations)
├─ 源实体(from)
├─ 目标实体(to)
└─ 关系类型(relationType)

观察层(Observations)
└─ 原子化事实信息(strings)

数据流程

1
2
3
用户输入 → 实体识别 → 知识图谱操作 → 持久化存储

查询请求 ← 信息检索 ← 关系遍历 ← 图谱加载 ←

存储机制

  • 格式:JSON
  • 位置:本地文件系统(Docker volume 或本地目录)
  • 持久化:自动保存所有更改
  • 并发:单实例写入,支持多实例读取

与其他 MCP 服务器对比

特性 Memory MCP Qdrant MCP SQLite MCP Redis MCP
数据结构 知识图谱 向量嵌入 关系型表 键值对
查询方式 图遍历 语义搜索 SQL Key查询
关系表达 ✅ 原生支持 ❌ 无 ⚠️ 外键 ❌ 无
语义搜索 ❌ 无 ✅ 原生支持 ❌ 无 ❌ 无
持久化 ✅ 本地文件 ✅ 数据库 ✅ 文件 ⚠️ 可选
扩展性 ⭐⭐⭐ 中 ⭐⭐⭐⭐⭐ 高 ⭐⭐⭐ 中 ⭐⭐⭐⭐ 高
适用场景 结构化记忆 RAG应用 事务数据 缓存
学习曲线 ⭐⭐⭐ 中 ⭐⭐⭐⭐ 高 ⭐⭐ 低 ⭐⭐ 低

选择建议

  • 需要表达复杂关系 → Memory MCP
  • 需要语义搜索 → Qdrant MCP
  • 需要事务和JOIN → SQLite MCP
  • 需要高速缓存 → Redis MCP

最佳实践

1. 实体命名规范

1
2
3
4
5
6
7
8
9
10
11
// 好的命名
"John_Doe"
"Acme_Corporation"
"AI_Research_Project"
"Python_Programming_Language"

// 避免的命名
"john" // 太简单,容易冲突
"user1" // 不够描述性
"John Doe" // 包含空格
"JOHNDOE" // 全大写不易读

2. 实体类型设计

建立清晰的类型层次:

1
2
3
4
5
6
7
8
9
10
11
// 基础类型
"person"
"organization"
"project"
"concept"

// 扩展类型
"employee" // 继承自 person
"customer" // 继承自 person
"company" // 继承自 organization
"open_source_project" // 继承自 project

3. 观察信息原则

1
2
3
4
5
6
7
8
9
10
11
12
// ✅ 好的观察(原子化)
"Lives in San Francisco"
"Prefers Python for backend development"
"Has 5 years of experience in data engineering"

// ❌ 不好的观察(复合信息)
"Lives in San Francisco and works remotely with 5 years of experience"

// 应该拆分为:
"Lives in San Francisco"
"Works remotely"
"Has 5 years of work experience"

4. 关系类型一致性

1
2
3
4
5
6
7
8
// 使用主动语态,保持一致性
"works_at" // 而不是 "employed_by"
"manages" // 而不是 "is_manager_of"
"owns" // 而不是 "has_ownership"

// 避免混用不同时态
"working_at" // ❌ 进行时
"works_at" // ✅ 一般现在时

5. 定期维护策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 清理过时信息
delete_observations({
deletions: [{
entityName: "User_Profile",
observations: ["Lives in New York"] // 已搬家
}]
})

// 更新关系
delete_relations({
relations: [{
from: "Employee",
to: "Old_Company",
relationType: "works_at"
}]
})

create_relations({
relations: [{
from: "Employee",
to: "New_Company",
relationType: "works_at"
}]
})

6. 会话开始时加载记忆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 系统提示词建议
const systemPrompt = `
You are an AI assistant with memory capabilities.

At the start of each conversation:
1. Use search_nodes to find relevant user information
2. Use open_nodes to retrieve complete user context
3. Reference stored preferences and history in your responses

Remember to:
- Update observations when user shares new information
- Maintain relationships between entities
- Respect user privacy and data retention preferences
`;

7. 批量操作优化

1
2
3
4
5
6
7
8
9
10
11
12
13
// ✅ 好的做法:批量创建
create_entities({
entities: [
{ name: "Entity1", entityType: "type1", observations: ["..."] },
{ name: "Entity2", entityType: "type2", observations: ["..."] },
{ name: "Entity3", entityType: "type3", observations: ["..."] }
]
})

// ❌ 避免:多次单个创建
create_entities({ entities: [{ name: "Entity1", ... }] })
create_entities({ entities: [{ name: "Entity2", ... }] })
create_entities({ entities: [{ name: "Entity3", ... }] })

常见问题

Q: 数据存储在哪里?

Memory Server 使用本地文件系统存储数据:

  • Docker: 存储在命名卷 claude-memory:/app/dist
  • NPX: 存储在项目目录或系统临时目录
  • 格式: JSON 格式的知识图谱文件

Q: 支持多用户吗?

目前是单用户设计,但可以通过实体类型和命名空间区分:

1
2
3
4
5
6
7
// 使用前缀区分用户
create_entities({
entities: [
{ name: "User1_Profile", entityType: "person", ... },
{ name: "User2_Profile", entityType: "person", ... }
]
})

Q: 如何备份和恢复数据?

1
2
3
4
5
6
7
8
9
# 导出知识图谱
# 使用 read_graph 工具获取完整数据,保存为 JSON

# Docker 备份
docker volume inspect claude-memory # 查看数据位置
# 复制数据文件进行备份

# 恢复
# 使用 create_entities 和 create_relations 重建图谱

Q: 性能如何?

  • 小规模 (<1000 实体): 毫秒级响应
  • 中等规模 (1000-10000 实体): 仍然快速
  • 大规模 (>10000 实体): 考虑使用专业图数据库

Q: 支持事务吗?

不支持 ACID 事务。如果需要事务保证,建议使用 SQLite MCP Server。

Q: 如何实现隐私保护?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 1. 敏感信息脱敏
create_entities({
entities: [{
name: "User_Profile",
entityType: "person",
observations: [
"Age range: 25-30", // 而不是具体年龄
"Location: California" // 而不是详细地址
]
}]
})

// 2. 定期清理
delete_observations({
deletions: [{
entityName: "User_Profile",
observations: ["Temporary preference: ..."]
}]
})

Q: 与向量数据库比较如何?

Memory Server 优势

  • 更适合表达明确的关系
  • 查询逻辑更直观
  • 无需嵌入模型

向量数据库(如 Qdrant)优势

  • 语义搜索能力强
  • 处理模糊查询更好
  • 适合大规模文本检索

混合使用

1
2
3
4
5
// 结构化记忆用 Memory Server
create_entities({ ... })

// 文本语义搜索用 Qdrant
qdrant_store({ information: "..." })

评分详情

维度 评分 说明
功能性 5.0/5.0 知识图谱功能完整,CRUD 操作齐全
文档质量 5.0/5.0 官方文档清晰,示例丰富
社区活跃度 5.0/5.0 官方维护,社区反馈积极
维护状态 5.0/5.0 持续更新,bug 修复及时
代码质量 5.0/5.0 TypeScript 实现,代码规范
综合评分 5.0/5.0 优秀的持久化记忆解决方案

总结

MCP Memory Server 是为 AI 助手提供长期记忆能力的理想选择。它采用知识图谱设计,能够优雅地表达实体关系和复杂信息结构。作为官方实现,它提供了可靠的持久化存储和完整的操作接口。

推荐指数: ⭐⭐⭐⭐⭐ (5/5)

适合你的情况

  • ✅ 需要跨会话记忆功能
  • ✅ 需要表达实体关系
  • ✅ 构建个性化对话系统
  • ✅ 追踪用户偏好和历史
  • ✅ 项目知识管理
  • ✅ 客户关系管理

不适合的情况

  • ❌ 需要语义搜索(用 Qdrant)
  • ❌ 需要 SQL 事务(用 SQLite)
  • ❌ 需要高速缓存(用 Redis)
  • ❌ 超大规模图数据(用 Neo4j)

实战示例

完整的用户记忆管理系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 1. 初始化用户记忆
async function initializeUserMemory(userId: string, userName: string) {
// 创建用户实体
await create_entities({
entities: [{
name: `User_${userId}`,
entityType: "person",
observations: [
`Display name: ${userName}`,
"First interaction: " + new Date().toISOString(),
"Status: active"
]
}]
})
}

// 2. 记录用户偏好
async function recordUserPreference(userId: string, preference: string) {
await add_observations({
observations: [{
entityName: `User_${userId}`,
contents: [preference]
}]
})
}

// 3. 建立项目关系
async function linkUserToProject(userId: string, projectName: string) {
await create_relations({
relations: [{
from: `User_${userId}`,
to: projectName,
relationType: "works_on"
}]
})
}

// 4. 查询用户上下文
async function getUserContext(userId: string) {
const result = await open_nodes({
names: [`User_${userId}`]
})
return result
}

// 5. 搜索相关信息
async function searchUsersBySkill(skill: string) {
const result = await search_nodes({
query: skill
})
return result
}

// 使用示例
await initializeUserMemory("123", "Alice")
await recordUserPreference("123", "Prefers dark mode")
await recordUserPreference("123", "Expert in Python")
await linkUserToProject("123", "DataPipeline_Project")

const context = await getUserContext("123")
console.log("User context:", context)

const pythonExperts = await searchUsersBySkill("Python")
console.log("Python experts:", pythonExperts)

相关资源


更新时间: 2025-10-14
数据来源: GitHub, 质量评分: 5.0/5.0
文档版本: 1.1

© 2026 Generative AI Discovery All Rights Reserved.
Theme by hiero