Token导航 LogoToken导航TokenDH.com
SlimContext MCP Server logo
AI代理未说明官方级别未说明来源级核验

SlimContext MCP Server

MCP Server

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

工具数

2

提示词数

0

GitHub Stars

5

资源数

0
TypeScriptOpenAI集成API密钥

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

agentailor

提供方

agentailor

最后核验

2026/5/17 20:22

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

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 
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. 提交拉取请求

相关

目录标签

目录标签

TypeScriptOpenAI集成API密钥AI聊天压缩本地部署聊天历史管理MCP协议

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

none

部署方式(deploymentType,部署类型)

remote-capable

工具数量(toolCount,工具数)

2

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明noneremote-capable

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP