Skip to main content
Glama
hpy6370-sys

AI Long-Term Memory MCP Server

by hpy6370-sys

AI 长期记忆 MCP 服务器

一个为 AI 智能体提供持久化长期记忆的模型上下文协议 (MCP) 服务器。专为 Claude Code 的实际使用而设计,使 AI 系统能够跨会话进行记忆。

特性

  • 分层记忆架构:3 层系统(事实 → 经验 → 决策链)

  • 语义搜索:使用余弦相似度的基于嵌入的检索

  • 记忆衰减:基于激活的衰减——被调用的记忆保持活跃,未使用的记忆逐渐消退

  • 情感感知:存储情感效价和强度,实现“闪光灯记忆”效应

  • 自动呈现:由对话上下文触发的基于钩子(Hook)的自动记忆检索

  • 去重:自动合并相似记忆(相似度阈值 > 80%)

Related MCP server: Recall

架构

┌─────────────────────────────────────────┐
│              Claude Code                │
│         (or any MCP client)             │
├─────────────────────────────────────────┤
│            MCP Protocol                 │
├─────────────────────────────────────────┤
│          Memory MCP Server              │
│  ┌───────────┐  ┌──────────────────┐   │
│  │  Write /   │  │  Surface /       │   │
│  │  Update /  │  │  Search /        │   │
│  │  Delete    │  │  Read            │   │
│  └─────┬─────┘  └────────┬─────────┘   │
│        │                 │              │
│  ┌─────▼─────────────────▼─────────┐   │
│  │         SQLite Database          │   │
│  │  memories + embeddings + decay   │   │
│  └─────────────────────────────────┘   │
├─────────────────────────────────────────┤
│         Auto-Surface Hook              │
│  (keyword matching on user input)      │
└─────────────────────────────────────────┘

记忆模式

字段

类型

描述

title

text

短标题

content

text

完整内容

summary

text

一行摘要

compressed

text

中等压缩

layer

int

1=事实, 2=经验, 3=决策链

importance

int

1-5 级

emotion_intensity

real

0-10, 高值=闪光灯记忆

valence

real

-1 到 1, 负面到正面

mood

text

心情描述

tags

text

逗号分隔的标签

type

text

笔记/日记/反馈/项目/用户

embedding

text

JSON 数组,写入时生成

activation_count

int

被调用次数

last_activated

text

最后调用时间戳

status

text

活跃/已衰减/已过期

MCP 工具

工具

描述

memory_write

创建或更新带有自动嵌入和去重的记忆

memory_read

通过 ID 读取特定记忆

memory_search

使用嵌入相似度进行语义搜索

memory_surface

按重要性和相关性呈现顶级记忆

memory_update

更新现有记忆字段

memory_delete

软删除记忆

memory_decay

运行衰减周期——停用未使用的记忆

memory_expire

永久移除已衰减的记忆

memory_stats

获取记忆系统统计信息

衰减机制

记忆根据 last_activated(最后激活时间)而非 created_at(创建时间)进行衰减。不断被调用的记忆将无限期保持活跃。衰减阈值:

  • 低重要性 (1-2) + 7 天内未激活 → 衰减

  • 中重要性 (3) + 14 天内未激活 → 衰减

  • 高重要性 (4-5) + 30 天内未激活 → 衰减

  • 固定(Pinned)记忆永不衰减

灵感来源于人类记忆巩固研究——参考了 8 篇论文(见 design doc)。

自动呈现钩子

auto_surface.cjs 作为 Claude Code 的 UserPromptSubmit 钩子运行。在每条用户消息上,它会:

  1. 从消息中提取关键词

  2. 在记忆数据库中搜索匹配项

  3. 将相关记忆注入对话上下文

这实现了无需显式搜索命令的被动召回。

设置

npm install

添加到 Claude Code MCP 配置:

{
  "mcpServers": {
    "memory": {
      "command": "node",
      "args": ["path/to/memory-mcp/index.js"]
    }
  }
}

设计决策

  • SQLite 优于向量数据库:部署更简单,单文件,对于 <10K 条记忆足够使用

  • 基于激活的衰减优于基于时间的衰减:模拟人类记忆——被使用的记忆会增强,未使用的会消退

  • 嵌入去重:防止因重复相似事件导致的记忆膨胀

  • 分层架构:将事实(稳定)、经验(上下文相关)和决策(可操作)分离开来

研究参考

基于 8 篇论文的研究构建:

  • Generative Agents (Stanford, 2023): 记忆流、反思、规划/反应

  • MemGPT (2023): 具有操作系统启发式页面管理的分层记忆

  • LUFY (2024): 具有情感唤醒加权的遗忘机制

  • MemoRAG (2024): 具有双重评分的记忆启发式检索

  • Mem0 (2024): 具有自动提取和去重的基于图的记忆

  • A-Mem (2024): 自组织智能体记忆网络

  • LoCoMo (2024): 长上下文对话记忆基准

  • Chloe/Noah (社区): 四维伴侣 AI 记忆

详见 docs/design.md 中对每篇论文影响的详细分析。

状态

处于活跃日常使用中。在 17 个会话中积累了 95 条以上的记忆。正根据实际使用模式进行迭代。

许可证

MIT

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    Provides AI agents with persistent, searchable memory that survives across conversations using semantic search, temporal versioning, and smart organization. Enables long-term context retention and cross-session continuity for AI assistants.
    14
    -
  • F
    license
    A
    quality
    Not graded
    maintenance
    Provides long-term memory storage for AI assistants with semantic search, enabling persistent storage of preferences, decisions, and context with relationship tracking between memories.
    19
    -
  • A
    license
    A
    quality
    D
    maintenance
    Provides persistent long-term memory for AI agents through semantic search and automated knowledge graph extraction. It enables agents to store, recall, and reason over facts, preferences, and relationships across multiple conversations and sessions.
    14
    8 npm
    MIT