AI聊天历史压缩工具

SlimContext MCP Server是一个提供AI聊天历史压缩工具的服务,支持基于令牌的修剪和AI驱动的摘要两种压缩策略,适用于需要优化聊天历史管理的场景。

  • by agentailor
  • TypeScript
  • 本地部署
  • 5 星标
  • 更新于
在 GitHub 查看 agentailor/slimcontext-mcp-server

使用说明

SlimContext MCP服务器

一个模型上下文协议(MCP)服务器,它封装了 SlimContext 库,为MCP兼容客户端提供AI聊天历史压缩工具。

概述

SlimContext MCP Server提供了两种强大的压缩策略作为MCP工具:

  1. trim_messages -基于令牌的压缩,在超过令牌阈值时删除最旧的消息
  2. summarize_messages -使用OpenAI进行AI驱动的压缩,以创建简洁的摘要

安装

npm install -g slimcontext-mcp-server
# or
pnpm add -g slimcontext-mcp-server

发展

# Clone and setup
git clone <repository>
cd slimcontext-mcp-server
pnpm install

# Build
pnpm build

# Run in development
pnpm dev

# Type checking
pnpm typecheck

配置

MCP客户端设置

添加到MCP客户端配置中:

{
  "mcpServers": {
    "slimcontext": {
      "command": "npx",
      "args": ["-y", "slimcontext-mcp-server"]
    }
  }
}

环境变量

  • OPENAI_API_KEY:用于摘要的OpenAI API键(可选,可作为工具参数传递)

工具

trim_消息

使用基于令牌的修剪策略压缩聊天历史记录。

参数:

  • messages (必填):聊天消息数组
  • maxModelTokens (可选):最大模型令牌上下文窗口(默认值:8192)
  • thresholdPercent (可选):触发压缩的百分比阈值0-1(默认值:0.7)
  • minRecentMessages (可选):要保留的最新消息数(默认值:2)

例子:

{
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "Hello!" },
    { "role": "assistant", "content": "Hi there! How can I help you today?" },
    { "role": "user", "content": "Tell me about AI." }
  ],
  "maxModelTokens": 4000,
  "thresholdPercent": 0.8,
  "minRecentMessages": 2
}

答复:

{
  "success": true,
  "original_message_count": 4,
  "compressed_message_count": 3,
  "messages_removed": 1,
  "compression_ratio": 0.75,
  "compressed_messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "assistant", "content": "Hi there! How can I help you today?" },
    { "role": "user", "content": "Tell me about AI." }
  ]
}

summarize_消息

使用AI支持的摘要策略压缩聊天历史记录。

参数:

  • messages (必填):聊天消息数组
  • maxModelTokens (可选):最大模型令牌上下文窗口(默认值:8192)
  • thresholdPercent (可选):触发压缩的百分比阈值0-1(默认值:0.7)
  • minRecentMessages (可选):要保留的最新消息数(默认值:4)
  • openaiApiKey (可选):OpenAI API密钥(也可以使用OpenAI_API_key env var)
  • openaiModel (可选):用于摘要的OpenAI模型(默认值:“gpt-4o-mini”)
  • customPrompt (可选):自定义摘要提示

例子:

{
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "I want to build a web scraper." },
    {
      "role": "assistant",
      "content": "I can help you build a web scraper! What programming language would you prefer?"
    },
    { "role": "user", "content": "Python please." },
    {
      "role": "assistant",
      "content": "Great choice! For Python web scraping, I recommend using requests and BeautifulSoup..."
    },
    { "role": "user", "content": "Can you show me a simple example?" }
  ],
  "maxModelTokens": 4000,
  "thresholdPercent": 0.6,
  "minRecentMessages": 2,
  "openaiModel": "gpt-4o-mini"
}

答复:

{
  "success": true,
  "original_message_count": 6,
  "compressed_message_count": 4,
  "messages_removed": 2,
  "summary_generated": true,
  "compression_ratio": 0.67,
  "compressed_messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    {
      "role": "system",
      "content": "The user expressed interest in building a web scraper and requested help with Python. The assistant recommended using requests and BeautifulSoup libraries for Python web scraping."
    },
    {
      "role": "assistant",
      "content": "Great choice! For Python web scraping, I recommend using requests and BeautifulSoup..."
    },
    { "role": "user", "content": "Can you show me a simple example?" }
  ]
}

消息格式

这两个工具都需要SlimContext格式的消息:

interface SlimContextMessage {
  role: 'system' | 'user' | 'assistant' | 'tool' | 'human';
  content: string;
}

错误处理

所有工具都返回结构化错误响应:

{
  "success": false,
  "error": "Error message description",
  "error_type": "SlimContextError" | "OpenAIError" | "UnknownError"
}

常见错误场景:

  • 缺少用于摘要的OpenAI API键
  • 消息格式无效
  • OpenAI API速率限制或错误
  • 参数值无效

令牌估计

SlimContext使用简单的启发式方法进行令牌估计: Math.ceil(content.length / 4) + 2这为大多数用例提供了合理的近似值。为了更准确地计数令牌,您需要在客户端应用程序中实现一个自定义令牌估计器。

压缩策略

修剪策略

  • 保留所有系统消息
  • 保留最近的N条消息
  • 删除最旧的非系统消息,直到低于令牌阈值
  • 快速和确定性
  • 没有外部API依赖项

总结策略

  • 保留所有系统消息
  • 保留最近的N条消息
  • 使用AI总结对话的中间部分
  • 创建上下文丰富的摘要
  • 需要OpenAI API访问

许可证

麻省理工学院

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 添加新功能的测试
  5. 提交拉取请求

相关