使用说明
内存MCP服务器
一个Python MCP服务器,在对话中为Claude Code提供持久内存。使用Claude从对话中提取偏好、代码模式和决策,将其嵌入句子转换器中,存储在LanceDB中,并通过向量相似性搜索进行检索。
工具
| 工具 | 说明 | API成本 |
|---|---|---|
save_conversation_memory(conversation_text) |
从对话中提取+嵌入+存储观察结果 | ~0.01-0.05美元 |
recall_memories(query, limit, category) |
搜索过去的观察结果+返回用户资料 | 免费 |
update_memory(observation_id, profile_key, new_value, delete) |
纠正/删除特定记忆 | 免费 |
分类 提取: coding_preference, communication_pattern, code_pattern, technical_context, decision, subtle_signal, project_context
设置
1.创建conda环境
conda create -n memory_mcp python=3.12 -y
conda activate memory_mcp
2.安装PyTorch
嵌入模型需要PyTorch。安装与您的硬件匹配的版本 之前 安装软件包:
- CPU(默认): 无需额外步骤--
pip install在步骤3中,将自动拉入与CPU兼容的PyTorch。 - 英伟达GPU: 从安装启用CUDA的构建https://pytorch.org/get-started/locally/以实现更快的嵌入。
- Jetson/其他设备: 首先为您的平台安装合适的车轮(例如检查https://developer.nvidia.com/embedded/downloadsJetson)。
3.安装包装
cd /path/to/memory_mcp
pip install -e ".[dev]"
默认嵌入模型(all-MiniLM-L6-v2)首次使用时自动下载(约80MB)。无需手动下载模型。
4.设置Anthropic API密钥
提取步骤调用Claude Sonnet来分析对话。您需要来自的API密钥https://console.anthropic.com/settings/keys.
echo "ANTHROPIC_API_KEY=sk-ant-your-key-here" > .env
5.用克劳德代码注册
claude mcp add memory -- /path/to/miniforge3/envs/memory_mcp/bin/python -m memory_mcp.server
将Python路径替换为实际的conda env路径。通过以下方式查找:
conda activate memory_mcp
which python
6.添加自动保存指令
添加以下内容 ~/.claude/CLAUDE.md (如果文件不存在,请创建该文件):
At the start of every conversation, call recall_memories with a query relevant to the user's first message to load context about their preferences and past decisions.
When the user asks how they would approach something, what they'd prefer, or asks you to consult memory, call recall_memories with a relevant query and use the returned profile + memories to give a personalized answer.
During a conversation, if you hit a decision point (e.g. choosing an architecture, picking a pattern, explaining a concept) where the user's past preferences or decisions could change the answer, call recall_memories with a focused query. Don't call it speculatively — only when you have a concrete reason to think stored context would shift your approach. Also, if a long stretch of the conversation has passed without a recall, consider calling it with a query relevant to the current topic — context from earlier sessions may have become relevant as the problem evolved.
At the end of every conversation, before closing, call save_conversation_memory with the full conversation text.
7.验证
重新启动克劳德代码。这 save_conversation_memory, recall_memories,以及 update_memory 工具应该可用。进行一次对话,让它保存在最后,然后开始一次新的对话,并检查记忆是否被回忆起来。
使用SessionEnd钩子自动保存(可选)
而不是依靠克劳德打电话 save_conversation_memory 在关闭之前,您可以使用 SessionEnd 挂钩,当您 /exit.
这 memory-save CLI命令与软件包一起安装。它直接读取会话记录,并在不通过MCP的情况下调用保存管道。
将此添加到 ~/.claude/settings.json:
{
"hooks": {
"SessionEnd": [
{
"hooks": [
{
"type": "command",
"command": "memory-save"
}
]
}
]
}
}
如果该文件中已有其他设置,请合并 hooks 键入您现有的配置。
它是如何工作的: 当你 /exit,Claude Code通过管道传输JSON对象 transcript_path 到stdin。 memory-save 阅读 .jsonl 转录,提取用户/助手消息,并运行完整的保存管道(通过Claude API+嵌入+LanceDB存储进行提取)。这将在会话结束后运行,因此没有延迟。
手动测试:
echo '{"transcript_path":"/path/to/transcript.jsonl"}' | memory-save
成绩单文件位于 ~/.claude/projects/ 在以项目路径命名的目录下。
没有GPU?没问题
如果没有GPU可用,嵌入模型会自动在CPU上运行。默认模型(all-MiniLM-L6-v2)它体积小,速度快——在CPU上嵌入不到一秒钟。
当检测到CUDA GPU时,它会自动用于更快的批处理嵌入。这 MEMORY_MCP_EMBEDDING_DEVICE 设置控制此操作:
auto(默认):使用GPU(如果可用),回退到CPUcpu:强制CPUcuda:强制GPU
配置
环境变量(前缀 MEMORY_MCP_ 或设置 .env):
| 变量 | 默认值 | 描述 |
|---|---|---|
ANTHROPIC_API_KEY |
-- | 必填。提取用无烟煤API键 |
MEMORY_MCP_EMBEDDING_MODEL |
all-MiniLM-L6-v2 |
句子转换器型号ID |
MEMORY_MCP_EMBEDDING_DIM |
384 |
嵌入向量维度(必须与模型匹配) |
MEMORY_MCP_EMBEDDING_DEVICE |
auto |
设备: auto, cpu,或 cuda |
MEMORY_MCP_GPU_MEMORY_THRESHOLD_GB |
4.0 |
使用CUDA的最小可用GPU内存 |
MEMORY_MCP_EXTRACTION_MODEL |
claude-sonnet-4-20250514 |
克劳德提取模型 |
MEMORY_MCP_DATA_DIR |
data/ |
数据目录 |
MEMORY_MCP_MODELS_DIR |
models/ |
模型缓存目录 |
MEMORY_MCP_LANCE_DB_PATH |
data/lancedb |
LanceDB存储路径 |
MEMORY_MCP_PROFILE_PATH |
data/user_profile.json |
用户配置文件JSON路径 |
MEMORY_MCP_DEFAULT_RECALL_LIMIT |
20 |
返回的默认内存数 |
MEMORY_MCP_RELEVANCE_THRESHOLD |
0.3 |
召回结果的最小相似性得分 |
运作原理
保存流量:
- Claude Sonnet通过流式提取分析对话,逐步发布观察结果
- 使用句子变换器将观测值嵌入384个模糊向量中
- LanceDB中存储的观测值+矢量
- 高置信度偏好(>=0.7)已合并到用户配置文件中
召回流程:
- 嵌入同一模型的查询
- LanceDB中的向量相似性搜索(余弦距离)
- 返回用户资料+排名高于相关性阈值的相关记忆
运行测试
conda activate memory_mcp
pytest tests/ -v
项目结构
memory_mcp/
├── pyproject.toml
├── .env # API key (gitignored)
├── src/memory_mcp/
│ ├── server.py # MCP server entry point (FastMCP, stdio)
│ ├── save_cli.py # CLI entry point for SessionEnd hook
│ ├── config.py # Settings with env var overrides
│ ├── models.py # Pydantic data models
│ ├── tools/
│ │ ├── save.py # save_conversation_memory
│ │ ├── recall.py # recall_memories
│ │ └── update.py # update_memory
│ ├── extraction/
│ │ └── extractor.py # Claude API streaming conversation analysis
│ ├── embeddings/
│ │ └── manager.py # Sentence-transformers embedding manager
│ └── storage/
│ ├── lance_store.py # LanceDB vector operations
│ └── profile.py # Aggregated user profile (JSON)
├── data/ # LanceDB data + profile (gitignored)
├── models/ # Cached model weights (gitignored)
└── tests/
