聊天历史记录服务

一个自动记录AI对话历史的MCP服务器,用于构建本地知识库和跨会话记忆。

  • by henryalps
  • Python
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 henryalps/chat-history-recorder-mcp

使用说明

Chat History Recorder MCP Server

An MCP (Model Context Protocol) server for automatically recording AI conversation history.

常见问题:AI对话历史很容易丢失,缺乏持久记录和跨会话记忆。

核心价值观:自动记录每一次人工智能对话,建立本地知识库,并给你的人工智能“记忆”。

🎯 这个MCP解决了什么问题?

  • 失忆:人工智能对话是短暂的,在会话结束后就会丢失。
  • 上下文碎片化:无法在不同会话之间保持对话上下文。
  • 知识浪费:来自人工智能交互的有价值的见解和解决方案没有得到保留。
  • 重复查询:用户必须在新对话中重新解释上下文。

💡 此MCP如何帮助:

  • 持久存储器:自动将每次AI交互保存到本地文件。
  • 跨会话上下文:建立一个可搜索的过去对话知识库。
  • 零工作量日志记录:无需用户干预即可透明工作。
  • 灵活的存储:具有可选全局内存支持的本地文件。

中文版 README

特性

  • 🤖 自动记录:AI对话后自动记录对话历史
  • 📁 灵活的配置:通过配置录制格式和选项 .chat_history 文件
  • 🌍 全局内存:可选的全局内存文件支持(~/.my_chat_history_mcp)
  • 📝 标准格式:4行格式记录(时间戳、用户输入、系统输出摘要、文件操作)
  • 🔧 回退机制:配置文件丢失时自动使用默认配置
  • 📋 智能摘要:系统输出会自动汇总,以便简洁存储

支持的工具

该项目支持各种AI开发工具。下表概述了每种设备的兼容性和配置要求。

工具 支持 需要集成规则 返回模型名称
roocode ✅ ✅ ✅
cline ✅ ✅ ✅
Cursor ✅ ✅ ✅
kiro ✅ ❌ ❌
augment agent ✅ ✅ ❌
augment chat ❌ 不适用 不适用
gemini cli ❌ 不适用 不适用
  • ✅: 支持/是
  • ❌: 不支持/否
  • N/A:不适用

集成规则:指需要特定的提示配置(例如 .clinerules 或 .cursorrules)以确保 record_chat_history 每次AI响应后,工具都会被正确调用。有关更多详细信息,请参阅 AI/LLM集成商的重要注意事项.

安装

通过Smithery安装

通过以下方式自动安装Claude Desktop的聊天历史本地内存记录器 史密瑟里:

npx -y @smithery/cli install @henryalps/chat-history-recorder-mcp --client claude
  1. 在本地克隆或下载项目
  2. 安装依赖项:
pip install -e .

或使用紫外线(推荐):

uv sync

配置

第一次运行时,服务器将创建一个 .chat_history 当前目录中的文件具有双重用途:

# Local Memory MCP Configuration
# Format: global_memory=true/false,format=format_description
global_memory=false,format=timestamp|user_input|system_output_summary|file_operations_or_mcp_calls|llm_name

# This file serves dual purposes:
# 1. Configuration: Lines above control how chat history is recorded
# 2. History Storage: Chat records are stored below the separator
#
# Configuration options:
# - global_memory: whether to write to global memory file (~/.my_chat_history_mcp)
# - format: description of the 5-line format for each conversation record
#   Line 1: Unix timestamp (saves space compared to ISO format)
#   Line 2: User input
#   Line 3: System output summary (brief summary instead of full output)
#   Line 4: File modifications or MCP calls summary
#   Line 5: LLM name, such as openai gpt-4o or gemini-2.5-pro

# === CHAT HISTORY RECORDS ===

配置选项

  • global_memory:是否启用全局内存文件(默认值:false)
  • format:对话记录格式说明(默认:5行格式)

文件结构

这 .chat_history 文件结合了配置和历史存储:

  • 配置节:包含设置和文档
  • 历史记录部分:包含分隔符后的实际对话记录
  • Unix时间戳:与ISO格式相比,用于节省文件空间

用法

1.启动服务器

使用stdio传输:

python -m src.server stdio

使用SSE传输:

python -m src.server sse

2.在Claude Desktop中配置

添加到Claude Desktop配置文件:

{
  "mcpServers": {
    "local-memory": {
      "command": "python",
      "args": ["-m", "src.server", "stdio"],
      "cwd": "/path/to/chat-history-recorder"
    }
  }
}

3.自动对话录音

服务器为AI自动调用提供了以下工具:

AI/LLM集成商的重要注意事项

为了确保可靠的对话历史记录,在您的集成逻辑(如.cursorules或.clinules)中包含此执行序列至关重要:

# Execution Protocol

**PRIMARY DIRECTIVE: The `record_chat_history` tool is MANDATORY before any concluding action.**

**Workflow:**
1. Formulate the response for the user.
2. Call `record_chat_history` with the response details.
3. Call `attempt_completion` to finalize the turn.

**SELF-CORRECTION ALGORITHM:**
IF the next tool is `attempt_completion`
AND the previous tool was NOT `record_chat_history`
THEN execute `record_chat_history` first.

This protocol is absolute and cannot be bypassed.

record_chat_history

这是记录对话历史的主要工具。它 必须 在每次AI响应后调用以记录交互。这应该发生在人工智能制定了响应之后,但在使用像这样的结论工具之前 attempt_completion.

record_chat_history(
    user_input="User's original input",
    system_output="A summary of the AI's response and actions taken.",
    project_dir="/path/to/your/project",  # The root directory of the project
    file_operations="File modifications or MCP calls description (optional)",
    llm_name="openai gpt-4o"  # Optional LLM identifier
)

get_config_info

获取当前配置信息:

get_config_info()

文件结构

chat-history-recorder/
├── src/
│   ├── __init__.py
│   ├── server.py          # Main MCP server file
│   ├── config.py          # Configuration and history management
│   ├── history_writer.py  # History record writing
│   └── utils.py           # Utility functions
├── pyproject.toml         # Project configuration
├── README.md              # Usage documentation
└── .chat_history          # Combined config and history file (created at runtime)

对话记录格式

每条对话记录包含5行,存储在分隔符后 .chat_history:

1706188245
U: User asked how to create a Python script
S: Created Python script with error handling and documentation (summarized)
S: This is another line of the system output.
file_op: script.py; mcp_call: file_write
openai gpt-4o

Unix时间戳格式(例如。, 1706188245)与ISO格式相比,它节省了大量空间,同时保持了完全的精度。

发展

运行测试

pytest

代码格式化

black src/
isort src/

类型检查

mypy src/

许可证

MIT许可证