这是一个使用本地知识图谱实现的持久化内存的基本示例。它使 Claude 能够在聊天过程中记住用户的信息,并通过一个教训系统从过去的错误中学习。
实体是知识图谱中的主要节点。每个实体包含:
示例:
{
"name": "John_Smith",
"entityType": "person",
"observations": ["Speaks fluent Spanish"]
}

关系定义了实体之间的有向连接。它们总是以主动语态存储,描述实体之间如何互动或相互关联。
示例:
{
"from": "John_Smith",
"to": "Anthropic",
"relationType": "works_at"
}

观察是对实体的具体信息片段。它们是:
示例:
{
"entityName": "John_Smith",
"observations": [
"Speaks fluent Spanish",
"Graduated in 2019",
"Prefers morning meetings"
]
}

教训是一种特殊的实体,用来捕捉关于错误及其解决方案的知识。每个教训包括:
示例:
{
"name": "NPM_VERSION_MISMATCH_01",
"entityType": "lesson",
"observations": [
"Error occurs when using incompatible package versions",
"Affects Windows environments specifically",
"Resolution requires version pinning"
],
"errorPattern": {
"type": "dependency",
"message": "Cannot find package @shadcn/ui",
"context": "package installation"
},
"metadata": {
"severity": "high",
"environment": {
"os": "windows",
"nodeVersion": "18.x"
},
"createdAt": "2025-02-13T13:21:58.523Z",
"updatedAt": "2025-02-13T13:22:21.336Z",
"frequency": 1,
"successRate": 1.0
},
"verificationSteps": [
{
"command": "pnpm add shadcn@latest",
"expectedOutput": "Successfully installed shadcn",
"successIndicators": ["added shadcn"]
}
]
}

create_entities
entities
(对象数组)
name
(字符串): 实体标识符entityType
(字符串): 类型分类observations
(字符串[]): 关联的观察create_relations
relations
(对象数组)
from
(字符串): 源实体名称to
(字符串): 目标实体名称relationType
(字符串): 主动语态的关系类型add_observations
observations
(对象数组)
entityName
(字符串): 目标实体contents
(字符串[]): 要添加的新观察delete_entities
entityNames
(字符串[])delete_observations
deletions
(对象数组)
entityName
(字符串): 目标实体observations
(字符串[]): 要删除的观察delete_relations
relations
(对象数组)
from
(字符串): 源实体名称to
(字符串): 目标实体名称relationType
(字符串): 关系类型read_graph
search_nodes
query
(字符串)open_nodes
names
(字符串[])create_lesson
lesson
(对象)
name
(字符串): 唯一标识符entityType
(字符串): 必须为 "lesson"observations
(字符串[]): 关于错误和解决方案的注释errorPattern
(对象): 错误详情
type
(字符串): 错误类别message
(字符串): 错误信息context
(字符串): 错误发生的位置stackTrace
(字符串, 可选): 堆栈跟踪metadata
(对象): 额外信息
severity
("low" | "medium" | "high" | "critical")environment
(对象): 系统详情frequency
(数字): 遇到的次数successRate
(数字): 解决方案成功率verificationSteps
(数组): 解决方案验证
command
(字符串): 执行的操作expectedOutput
(字符串): 期望的结果successIndicators
(字符串[]): 成功标志find_similar_errors
errorPattern
(对象)
type
(字符串): 错误类别message
(字符串): 错误信息context
(字符串): 错误上下文update_lesson_success
lessonName
(字符串): 要更新的课程success
(布尔值): 解决方案是否有效get_lesson_recommendations
context
(字符串)服务器现在处理两种类型的文件:
memory.json
: 存储基本实体和关系lesson.json
: 存储带有错误模式的课程实体如果文件超过1000行,将自动拆分以保持性能。
要将此内存服务器与Cursor MCP客户端集成,请按照以下步骤操作:
git clone [repository-url]
cd [repository-name]

pnpm install
pnpm build
/path/to/the/dist/index.js
node /path/to/the/dist/index.js
Ctrl+Shift+P
现在,内存服务器应该已经与您的Cursor MCP客户端集成并可以使用了。
将以下内容添加到您的 claude_desktop_config.json
文件中:
{
"mcpServers": {
"memory": {
"command": "docker",
"args": ["run", "-i", "-v", "claude-memory:/app/dist", "--rm", "mcp/memory"]
}
}
}

{
"mcpServers": {
"memory": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-memory"
]
}
}
}

服务器可以通过以下环境变量进行配置:
{
"mcpServers": {
"memory": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-memory"
],
"env": {
"MEMORY_FILE_PATH": "/path/to/custom/memory.json"
}
}
}
}

MEMORY_FILE_PATH
: 内存存储 JSON 文件的路径(默认:服务器目录中的 memory.json
)利用记忆的提示取决于使用场景。更改提示有助于模型确定创建记忆的频率和类型。
这里是一个用于聊天个性化的示例提示。您可以在 Claude.ai 项目 的“自定义指令”字段中使用此提示。
Follow these steps for each interaction: 1. User Identification: - You should assume that you are interacting with default_user - If you have not identified default_user, proactively try to do so. 2. Memory Retrieval: - Always begin your chat by saying only "Remembering..." and retrieve all relevant information from your knowledge graph - Always refer to your knowledge graph as your "memory" 3. Memory - While conversing with the user, be attentive to any new information that falls into these categories: a) Basic Identity (age, gender, location, job title, education level, etc.) b) Behaviors (interests, habits, etc.) c) Preferences (communication style, preferred language, etc.) d) Goals (goals, targets, aspirations, etc.) e) Relationships (personal and professional relationships up to 3 degrees of separation) 4. Memory Update: - If any new information was gathered during the interaction, update your memory as follows: a) Create entities for recurring organizations, people, and significant events b) Connect them to the current entities using relations b) Store facts about them as observations
Docker:
docker build -t mcp/memory -f src/memory/Dockerfile .
该 MCP 服务器根据 MIT 许可证授权。这意味着您可以自由地使用、修改和分发该软件,但需遵守 MIT 许可证的条款和条件。更多详情,请参阅项目仓库中的 LICENSE 文件。
create_lesson
lesson
(对象)
find_similar_errors
errorPattern
(对象)
update_lesson_success
lessonName
(字符串): 要更新的课程success
(布尔值): 解决方案是否有效get_lesson_recommendations
context
(字符串)非常感谢!
https://github.com/modelcontextprotocol/servers
jerome3o-anthropic
https://github.com/modelcontextprotocol/servers/tree/main/src/memory