Knowledge Graph Memory Server

by T1nker-1220
Verified

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

知识图谱内存服务器

使用本地知识图谱实现持久内存的基本实现。这使得 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"] } ] }

API

工具

  • 创建实体
    • 在知识图谱中创建多个新实体
    • 输入: entities (对象数组)
      • 每个对象包含:
        • name (字符串):实体标识符
        • entityType (字符串):类型分类
        • observations (string[]):相关观察
    • 忽略具有现有名称的实体
  • 创建关系
    • 在实体之间创建多个新关系
    • 输入: relations (对象数组)
      • 每个对象包含:
        • from (字符串):源实体名称
        • to (字符串):目标实体名称
        • relationType (字符串):主动语态的关系类型
    • 跳过重复关系
  • 添加观察结果
    • 向现有实体添加新观察
    • 输入: observations (对象数组)
      • 每个对象包含:
        • entityName (字符串):目标实体
        • contents (string[]):要添加的新观察结果
    • 返回每个实体添加的观察值
    • 如果实体不存在则失败
  • 删除实体
    • 删除实体及其关系
    • 输入: entityNames (string[])
    • 级联删除关联关系
    • 如果实体不存在则静默操作
  • 删除观察结果
    • 从实体中删除特定观察结果
    • 输入: deletions (对象数组)
      • 每个对象包含:
        • entityName (字符串):目标实体
        • observations (string[]):要删除的观察结果
    • 如果不存在观察,则静默运行
  • 删除关系
    • 从图中删除特定关系
    • 输入: relations (对象数组)
      • 每个对象包含:
        • from (字符串):源实体名称
        • to (字符串):目标实体名称
        • relationType (字符串):关系类型
    • 如果关系不存在则静默操作
  • 读取图
    • 阅读整个知识图谱
    • 无需输入
    • 返回包含所有实体和关系的完整图形结构
  • 搜索节点
    • 根据查询搜索节点
    • 输入: query (字符串)
    • 搜索范围:
      • 实体名称
      • 实体类型
      • 观察内容
    • 返回匹配的实体及其关系
  • 打开节点
    • 按名称检索特定节点
    • 输入: names (string[])
    • 返回:
      • 请求的实体
      • 被请求实体之间的关系
    • 默默跳过不存在的节点

课程管理工具

  • 创建课程
    • 从错误及其解决方案创建新课程
    • 输入: lesson (对象)
      • 包含:
        • name (字符串):唯一标识符
        • entityType (字符串):必须是“lesson”
        • observations (string[]):关于错误和解决方案的注释
        • errorPattern (对象):错误详情
          • type (字符串):错误类别
          • message (字符串):错误消息
          • context (字符串):错误发生的位置
          • stackTrace (字符串,可选):堆栈跟踪
        • metadata (对象):附加信息
          • severity (“低” | “中” | “高” | “严重”)
          • environment (对象):系统详细信息
          • frequency (数字):遇到的次数
          • successRate (数字):解决方案成功率
        • verificationSteps (数组):解决方案验证
          • 每个步骤包含:
            • command (字符串):要采取的操作
            • expectedOutput (字符串):预期结果
            • successIndicators (string[]): 成功标记
    • 自动初始化元数据时间戳
    • 验证所有必填字段
  • 查找类似错误
    • 查找类似错误及其解决方案
    • 输入: errorPattern (对象)
      • 包含:
        • type (字符串):错误类别
        • message (字符串):错误消息
        • context (字符串):错误上下文
    • 返回按成功率排序的匹配课程
    • 使用模糊匹配来显示错误消息
  • 更新课程成功
    • 更新课程的成功跟踪
    • 输入:
      • lessonName (字符串):要更新的课程
      • success (布尔值):解决方案是否有效
    • 更新:
      • 成功率(加权平均)
      • 频率计数器
      • 上次更新时间戳
  • 获取课程建议
    • 获取与当前情况相关的课程
    • 输入: context (字符串)
    • 搜索范围:
      • 错误类型
      • 错误信息
      • 错误上下文
      • 课堂观察
    • 返回按以下方式排序的课程:
      • 上下文相关性
      • 成功率
    • 包含完整的解决方案详细信息

文件管理

服务器现在处理两种类型的文件:

  • memory.json :存储基本实体和关系
  • lesson.json :存储具有错误模式的课程实体

如果文件超过 1000 行,则会自动拆分以保持性能。

Cursor MCP 客户端设置

要将此内存服务器与 Cursor MCP 客户端集成,请按照以下步骤操作:

  1. 克隆存储库:
git clone [repository-url] cd [repository-name]
  1. 安装依赖项:
pnpm install
  1. 构建项目:
pnpm build
  1. 配置服务器:
  • 找到构建的服务器文件的完整路径: /path/to/the/dist/index.js
  • 使用 Node.js 启动服务器: node /path/to/the/dist/index.js
  1. 在光标中激活:
  • 使用键盘快捷键Ctrl+Shift+P
  • 输入“重新加载窗口”并选择它
  • 等待几秒钟,让 MCP 服务器激活
  • 出现提示时选择 stdio 类型

内存服务器现在应该与您的 Cursor MCP 客户端集成并可供使用。

与 Claude Desktop 一起使用

设置

将其添加到您的 claude_desktop_config.json 中:

Docker

{ "mcpServers": { "memory": { "command": "docker", "args": ["run", "-i", "-v", "claude-memory:/app/dist", "--rm", "mcp/memory"] } } }

NPX

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

具有自定义设置的 NPX

可以使用以下环境变量配置服务器:

{ "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 文件。

新工具

  • 创建课程
    • 从错误及其解决方案创建新课程
    • 输入: lesson (对象)
      • 包含错误模式、解决步骤和元数据
      • 自动跟踪创建时间和更新
      • 验证解决方案步骤是否完成
  • 查找类似错误
    • 查找类似错误及其解决方案
    • 输入: errorPattern (对象)
      • 包含错误类型、消息和上下文
      • 返回按成功率排序的匹配课程
      • 包含相关解决方案和验证步骤
  • 更新课程成功
    • 更新课程的成功跟踪
    • 输入:
      • lessonName (字符串):要更新的课程
      • success (布尔值):解决方案是否有效
    • 更新成功率和频率指标
  • 获取课程建议
    • 获取与当前情况相关的课程
    • 输入: context (字符串)
    • 返回按相关性和成功率排序的课程
    • 包含完整的解决方案细节和验证步骤

非常感谢这个 repo 的所有者提供的基础代码,我通过课程和文件管理增强了它

非常感谢! https://github.com/modelcontextprotocol/servers jerome3o-anthropic https://github.com/modelcontextprotocol/servers/tree/main/src/memory

ID: eoinvr1bz0