关于 Claude Context Sync
Claude Context Sync 是用于协调并行 Claude Code 会话的 MCP 服务,可共享会话摘要、检测文件和符号冲突,并支持会话间消息传递。
主要功能
- 01共享结构化会话上下文
- 02实时检测文件编辑冲突
- 03识别符号级冲突
- 04在会话间发送消息
- 05按需汇总过期会话
- 06会话结束时清理跟踪数据
适用场景
- 01协调并行执行的 Claude Code 任务
- 02在编辑冲突发生前发现重叠修改
- 03跨会话共享决策和变更文件
- 04无需切换终端即可向其他会话传递上下文
MCP 相关
Claude Code 通过 stdio 自动启动该服务。六个 MCP 工具可列出会话、获取上下文和变更、交换消息及解决冲突。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
6收录的工具列表 · 更新于 3 周前
此工具无需参数。
详细介绍
claude-context-sync
一个 MCP 服务,可自动汇总并在多个并行的 Claude Code 会话间共享上下文。
当你在不同任务上运行多个 Claude Code 会话时,每个会话都无法了解其他会话正在做什么。claude-context-sync 会监视会话日志,按需生成结构化摘要,并提供实时文件冲突检测和跨会话消息传递功能。
功能
- 会话上下文共享 — 任何会话都可以查看其他会话正在处理的内容
- 文件冲突检测 — 多个会话编辑同一文件时发出实时警报
- 符号级冲突检测 — 检测共享的函数、类和接口编辑(TypeScript、Python、Swift、Go、Rust)
- 跨会话消息传递 — 无需切换终端即可在会话间发送消息
- 惰性求值 — 未调用工具时零开销
- 自动清理 — 会话结束时移除已跟踪的数据
安装
npm install -g claude-context-sync
context-sync init
就这样。下次打开 Claude Code 时,MCP 服务会自动启动。init 命令还会注册一个冲突检测钩子。
MCP 工具
list_sessions
返回处于活动状态的 Claude Code 会话及其当前状态、任务摘要和任何文件冲突。
get_session_context
返回指定会话的完整结构化摘要。如果摘要已过时,会自动重新生成。包含冲突信息。
参数:sessionId(string)
get_all_changes
返回所有会话中已更改的文件和决策,并可按项目筛选。
参数:project(string,可选)
send_message
向另一个会话发送消息。省略 targetSessionId 可向同一项目中的所有会话广播消息。
参数:fromSessionId(string)、message(string)、project(string)、targetSessionId(string,可选)
get_messages
返回某个会话的消息历史记录。
参数:sessionId(string)、unreadOnly(boolean,可选)
resolve_conflicts
从冲突跟踪列表中清除已解决的文件。
参数:sessionId(string)、files(string[])
工作原理
上下文共享
[Always running] Watcher: detects .jsonl changes → sets stale flag (zero cost)
↓
[On tool call] MCP Tool invoked → summarizes only stale sessions
↓
Extractor → Generator (claude -p) → Store → Response
冲突检测
[Always running] Watcher: detects .jsonl changes
↓
FileTracker: parses Write/Edit tool_use events
↓
SymbolResolver: identifies enclosing function/class/type
↓
Writes per-session touched files
[Every message] Hook (UserPromptSubmit): checks for new conflicts
↓
Alerts Claude once per new conflict → Claude notifies user
消息传递
Session A: send_message → writes to ~/.claude/context-sync/messages/{targetId}/
↓
Session B: Hook detects unread message → injects into AI context
↓
Claude reads and relays to user → marks as read
架构
单个 MCP 服务进程,无需单独的守护进程。Claude Code 会通过 stdio 传输自动启动它。
src/
├── mcp/server.ts # MCP server + 6 tools
├── watcher/
│ ├── watcher.ts # File watching (chokidar)
│ ├── scheduler.ts # Hybrid summarization strategy
│ ├── file-tracker.ts # Real-time file edit tracking
│ └── symbol-resolver.ts # Pattern-based symbol detection
├── summarizer/
│ ├── extractor.ts # .jsonl parsing + stratified sampling
│ └── generator.ts # claude -p invocation
├── messaging/
│ └── messenger.ts # Cross-session message passing
├── store/store.ts # Local summary cache
├── cli.ts # CLI (init + serve + hook registration)
└── types.ts # Shared types
摘要生成策略
- 增量:使用新消息更新现有摘要(默认)
- 完整:从头重新生成(每进行 4 次增量更新后)
- 分层采样:对于大型会话(500 条以上消息),对开头、中间和结尾进行采样
- 缓存:如果内容未发生变化,则返回已存储的摘要(零开销)
支持的语言(符号检测)
| 语言 | 检测到的符号 |
|---|---|
| TypeScript/JavaScript | function、class、interface、type、enum、const |
| Python | def、class |
| Swift | func、class、struct、protocol、enum、extension |
| Go | func、type(struct/interface) |
| Rust | fn、struct、enum、trait、impl |
不支持的文件类型会回退到文件级冲突检测。
要求
- Node.js 20+
- Claude Code CLI(可使用
claude命令) jq(用于冲突检测钩子)
反馈
我们很乐意听取你的意见!如果你有功能需求、错误报告,或者只是想分享你如何使用 claude-context-sync:
开发
git clone https://github.com/ybin4548/claude-context-sync.git
cd claude-context-sync
npm install
npm run build
npm test
许可证
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。