关于 slimcontext-mcp-server
slimcontext-mcp-server 将 SlimContext 的聊天历史压缩能力封装为 MCP 服务,供兼容客户端调用。它提供基于令牌裁剪和借助 OpenAI 摘要两种策略。
主要功能
- 01按令牌阈值裁剪历史
- 02使用 OpenAI 生成摘要
- 03保留系统消息和近期对话
- 04返回结构化压缩结果
适用场景
- 01缩短超出上下文窗口的对话
- 02通过摘要保留长对话要点
MCP 相关
该 MCP 服务向兼容客户端提供 trim_messages 和 summarize_messages 两个聊天历史压缩工具。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
2收录的工具列表 · 更新于 2 周前
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| messages | array | 是 | 要压缩的聊天消息数组 |
| maxModelTokens | number | 否 | 模型的最大令牌上下文窗口 |
| thresholdPercent | number | 否 | 触发压缩的百分比阈值(0-1) |
| minRecentMessages | number | 否 | 始终保留的近期消息最少数量 |
详细介绍
SlimContext MCP 服务
一个 Model Context Protocol(MCP)服务,封装了 SlimContext 库,为兼容 MCP 的客户端提供 AI 聊天历史压缩工具。
概述
SlimContext MCP 服务将两种强大的压缩策略作为 MCP 工具提供:
trim_messages- 基于令牌的压缩,超出令牌阈值时移除最早的消息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
使用基于令牌的裁剪策略压缩聊天历史。
参数:
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_messages
使用 AI 驱动的摘要策略压缩聊天历史。
参数:
messages(必需):聊天消息数组maxModelTokens(可选):模型上下文窗口的最大令牌数(默认值:8192)thresholdPercent(可选):触发压缩的百分比阈值,范围为 0-1(默认值:0.7)minRecentMessages(可选):保留的近期消息最小数量(默认值:4)openaiApiKey(可选):OpenAI API 密钥(也可使用 OPENAI_API_KEY 环境变量)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
许可证
MIT
贡献
- Fork 此仓库
- 创建功能分支
- 完成修改
- 为新功能添加测试
- 提交拉取请求
相关项目
- SlimContext - 底层压缩库
- Model Context Protocol - 协议规范
- MCP SDK - 用于 MCP 的 TypeScript SDK
评论
支持 Markdown
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。