Skip to main content
Glama

session-migrator

English | 中文

跨代理会话记忆迁移层:根据目标模型的上下文窗口容量自动迁移或压缩对话。

解决什么问题

代理 1 正在进行的对话需要交接给代理 2,但两个代理使用不同的模型,上下文窗口也不同。规则很简单:

  • 如果目标模型能容纳整个对话 → 原样迁移,不压缩;

  • 如果容纳不下 → 只保留最有价值的上下文(优先保留最新消息)。

Related MCP server: OpenAI Assistant MCP Server

目录结构

session-migrator/
├── session_migrator/
│   ├── context_windows.py   # model capacity mapping table (the soul)
│   ├── exporter.py          # session export/serialization + token estimation
│   ├── decision.py          # decision engine: compare capacity → direct/compress
│   ├── compressors.py       # compressor: budget truncation, keeps latest
│   ├── storage.py           # shared storage: JSON files, per-workspace isolation
│   ├── codex_adapter.py     # Codex session → Session adapter
│   ├── llm_summarizer.py    # LLM topic summarization (deepseek/OpenAI-compatible)
│   ├── server.py            # MCP server entry (exposes migration tools)
│   └── __init__.py
├── examples/
│   ├── demo.py                     # full demo, zero dependencies
│   ├── codex_to_workbuddy_demo.py  # Codex → memory (truncation)
│   └── llm_summarize_demo.py       # Codex → memory (LLM topic summarization)
├── tests/test_core.py       # core logic tests
├── pyproject.toml
├── requirements.txt
└── LICENSE

快速开始

1. 先运行核心逻辑(零依赖)

python examples/demo.py
python tests/test_core.py

两者都只使用标准库。无需安装——你立刻就能看到"决策 + 压缩 + 存储"端到端地工作。

2. 作为 MCP 服务器运行

pip install mcp
python -m session_migrator.server

3. 连接到任意 MCP 客户端

以 Claude Code 为例,将其添加到项目的 .mcp.json(或你的全局配置)中:

{
  "mcpServers": {
    "session-migrator": {
      "command": "python",
      "args": ["-m", "session_migrator.server"]
    }
  }
}

Cursor / Codex / WorkBuddy 或任何支持 MCP stdio 的客户端都可以用同样的方式工作。连接后,代理可以调用 model_context_windowlist_known_modelsmigrate_session

4. 配置 LLM API(仅"主题摘要"需要)

要将 Codex 会话压缩为结构化记忆,你需要一个兼容 OpenAI 的 LLM。deepseek / OpenAI / 任何兼容 /chat/completions 的服务都可以——只需设置一个环境变量:

export DEEPSEEK_API_KEY="sk-xxx"          # or OPENAI_API_KEY

三个核心 MCP 工具不需要它(它们只做决策/截断压缩,不调用 LLM)。

MCP 工具

工具

用途

model_context_window(model)

查询模型的上下文窗口容量

list_known_models()

列出内置模型及其容量

migrate_session(messages_json, source_model, target_model, ...)

执行迁移,返回决策 + 迁移后的消息 + 迁移前后的 token 数

migrate_sessionmessages_json 格式如下:

[{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]

核心概念

决策引擎 decide(session, target_model)

判断标准是"目标容量能否容纳会话的实际 token 数",而不是简单地比较两个模型的容量——即使目标容量小于源模型,小会话仍然原样迁移。

压缩器 TruncationCompressor

默认实现零外部依赖:从最新消息开始向前保留完整消息,省略放不下的更早消息,并在顶部插入一条占位说明(省略数量 + 最早消息的预览)。

主题摘要(Codex → 记忆)

将 Codex 会话迁移为结构化记忆的完整流程(适配器 + LLM):

from session_migrator.codex_adapter import get_thread_meta, extract_rollout
from session_migrator.llm_summarizer import summarize_session

meta = get_thread_meta("your-codex-thread-id")
session = extract_rollout(meta["rollout_path"], meta["id"], meta["model"])
markdown = summarize_session(session, meta, target_chars=5000)  # needs LLM key set first

非 LLM 截断版本:codex_adapter.to_memory_markdown(session, meta)

模型容量表

session_migrator/context_windows.py 附带一个静态映射表(OpenAI / Anthropic / Google / 国产模型)。注意:这些是静态回退值,可能随提供商更新而变化。

路线图

  • LLM 主题摘要(llm_summarizer.py,见"主题摘要")

  • 动态容量获取(调用各提供商的 /models API)

  • 可逆压缩(可恢复原始文本)

  • 向量存储检索注入(按需检索)

  • 使用 tiktoken 精确计算 token

许可证

MIT

A
license - permissive license
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • F
    license
    Not graded
    quality
    Not graded
    maintenance
    Enables Claude and other MCP-compatible tools to communicate with OpenAI's GPT models (GPT-5, GPT-5-mini, o3) with conversation history and session management. Features advanced controls like reasoning effort settings, token tracking, and parallel conversation sessions for efficient AI workflows.
    9
  • A
    license
    Not graded
    quality
    C
    maintenance
    Provides context compression via the tokenslim engine, enabling MCP hosts to reduce token usage while preserving key information. Offers compress, retrieve, and stats tools for managing compressed content.
    Apache 2.0

View all related MCP servers

Related MCP Connectors

  • Hosted MCP server for LLM cost estimation, model comparison, and budget-aware routing.

  • Free OpenAI-compatible inference with signed provenance receipts and 3 focused MCP tools.

  • Remote MCP for Gemini upgrade evals, prompt regressions, output diffs, and eval receipts.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/liangyuan0219/session-migrator'

If you have feedback or need assistance with the MCP directory API, please join our Discord server