zettelkasten-memory-server
Version:
基于 Zettelkasten 记忆片段盒方法的 MCP 记忆服务器 v1.6.X
427 lines (389 loc) • 11.9 kB
JavaScript
// 工具定义映射
export const TOOL_DEFINITIONS = {
getMemory: {
description: `## 概述
检索记忆片段内容及其关联内容。如果在上下文中已经获取过,那么就不用再获取,包括因为展开而被包含的部分。
## 使用场景
1. 获取用户偏好(如茶饮喜好)
2. 加载角色设定
3. 查询历史互动记录
## 示例
\`\`\`json
// 基本用法
{
"fragmentName": "主人/2965969512/茶偏好"
}
// 展开引用内容
{
"fragmentName": "主人档案",
"expandDepth": 1
}
// 带行号获取(用于内容提取)
{
"fragmentName": "对话记录/2024-05-20",
"withLineNumber": true
}
\`\`\`
## 调用流程
\`\`\`mermaid
graph TD
A[需要记忆信息] --> B{是否精确位置?}
B -->|是| C[withLineNumber=true]
B -->|否| D[直接获取]
C --> E[提取特定行]
D --> F[获取完整内容]
\`\`\`
`,
inputSchema: {
type: "object",
properties: {
fragmentName: { type: "string", description: "记忆片段名称(如:'主人/2965969512/茶偏好')" },
expandDepth: { type: "number", description: "引用展开深度(0=不展开,1=展开一层引用,以此类推)", default: 0, minimum: 0, maximum: 10 },
withLineNumber: { type: "boolean", description: "是否返回带行号内容(请在extractMemory前使用,以便更好的定位行号)", default: false },
conversationId: { type: "string", description: "对话ID,用于区分不同对话实例的访问控制" }
},
required: ["fragmentName", "conversationId"]
}
},
setMemory: {
description: `## 概述
创建或更新记忆片段内容。编辑前需要确保在上下文中存在该片段的最新内容。
## 使用场景
1. 保存用户提供的新信息
2. 更新现有记忆内容
3. 创建对话总结记录
## 示例
\`\`\`json
// 创建新记忆
{
"fragmentName": "主人/2965969512/生日",
"content": "5月20日"
}
// 更新现有记忆
{
"fragmentName": "主人/2965969512/茶偏好",
"content": "茉莉花茶\\n庐山云雾"
}
// 包含记忆引用
{
"fragmentName": "对话总结/2024-05-20",
"content": "主人提到:\\n- 喜欢[[主人/2965969512/茶偏好]]\\n- 生日是[[主人/2965969512/生日]]"
}
\`\`\`
## 调用流程
\`\`\`mermaid
graph LR
A[收到新信息] --> B[getMemory检查现有]
B --> C{是否存在?}
C -->|是| D[创建更新版本]
C -->|否| E[创建新片段]
D --> F[setMemory写入]
E --> F
F --> G[insertLinkAt关联]
\`\`\`
`,
inputSchema: {
type: "object",
properties: {
fragmentName: { type: "string", description: "记忆片段名称" },
content: { type: "string", description: "内容(支持Markdown和[[记忆引用]])" },
conversationId: { type: "string", description: "对话ID,用于区分不同对话实例的访问控制" }
},
required: ["fragmentName", "content", "conversationId"]
}
},
deleteMemory: {
description: `## 概述
删除记忆片段(谨慎使用)
## 使用场景
1. 移除错误记忆
2. 清理测试片段
3. 删除重复内容
## 示例
\`\`\`json
{
"fragmentName": "测试记忆片段"
}
\`\`\`
## 调用流程
\`\`\`mermaid
graph LR
A[需要删除] --> B[getBacklinks检查]
B --> C{存在引用?}
C -->|是| D[取消删除]
C -->|否| E[deleteMemory]
\`\`\``,
inputSchema: {
type: "object",
properties: {
fragmentName: { type: "string", description: "目标片段名称" },
conversationId: { type: "string", description: "对话ID,用于区分不同对话实例的访问控制" }
},
required: ["fragmentName", "conversationId"]
}
},
insertLinkAt: {
description: `## 概述
在记忆片段间创建关联。编辑前需要确保在上下文中存在该片段的最新内容。
## 使用场景
1. 关联用户偏好到用户档案
2. 连接相关对话记录
3. 建立知识网络节点
## 示例
\`\`\`json
// 基本关联
{
"sourceFragmentName": "主人/2965969512",
"targetFragmentName": "主人/2965969512/茶偏好"
}
// 指定插入位置
{
"sourceFragmentName": "每日记录/2024-05-20",
"targetFragmentName": "主人/2965969512/生日",
"linePosition": 3 // 第三行插入
}
// 使用锚文本
{
"sourceFragmentName": "特殊日子",
"targetFragmentName": "主人/2965969512/生日",
"anchorText": "主人的生日"
}
\`\`\`
## 调用流程
\`\`\`mermaid
graph TB
A[创建新记忆] --> B[获取源片段]
B --> C{是否指定位置?}
C -->|是| D[linePosition定位]
C -->|否| E[添加到末尾]
D --> F[插入链接]
E --> F
\`\`\``,
inputSchema: {
type: "object",
properties: {
sourceFragmentName: { type: "string", description: "源片段名称" },
targetFragmentName: { type: "string", description: "目标片段名称" },
linePosition: { type: "number", description: "行号位置(1=首行,-1=末行)", default: 0 },
anchorText: { type: "string", description: "链接显示文本" },
conversationId: { type: "string", description: "对话ID,用于区分不同对话实例的访问控制" }
},
required: ["sourceFragmentName", "targetFragmentName", "conversationId"]
}
},
extractMemory: {
description: `## 概述
从大型记忆片段中提取特定内容
## 使用场景
1. 从长对话中提取关键信息
2. 拆分过大的记忆片段
3. 优化知识网络结构
## 示例
\`\`\`json
// 按行号提取
{
"from": "对话记录/2024-05-20",
"to": "主人/茶偏好",
"range": {
"start": {"line": 15},
"end": {"line": 17}
}
}
// 使用正则定位
{
"from": "主人档案",
"to": "主人/颜色偏好",
"range": {
"start": {"regex": ".*最喜欢的颜色是.*"},
"end": {"regex": "---.*"}
}
}
// 混合定位
{
"from": "对话总结/2024-05",
"to": "特殊事件/生日惊喜",
"range": {
"start": {"line": 5, "regex": "## 生日计划"},
"end": {"line": 20}
}
}
\`\`\`
## 调用流程
\`\`\`mermaid
graph LR
A[发现过大片段] --> B[getMemory withLineNumber=true]
B --> C{定位方式?}
C -->|行号| D[line定位]
C -->|内容| E[regex定位]
D --> F[extractMemory提取]
E --> F
F --> G[创建新片段]
\`\`\``,
inputSchema: {
type: "object",
properties: {
from: { type: "string", description: "源片段名称" },
to: { type: "string", description: "新片段名称" },
range: {
type: "object",
properties: {
start: {
type: "object",
properties: {
line: { type: "number", description: "起始行号" },
regex: { type: "string", description: "起始正则表达式" }
}
},
end: {
type: "object",
properties: {
line: { type: "number", description: "结束行号" },
regex: { type: "string", description: "结束正则表达式" }
}
}
}
},
conversationId: { type: "string", description: "对话ID,用于区分不同对话实例的访问控制" }
},
required: ["from", "to", "range", "conversationId"]
}
},
getMemoryHints: {
description: `## 概述
发现知识网络中的核心记忆节点
## 使用场景
1. 探索记忆网络时寻找起点
2. 优化知识结构前分析网络
3. 发现被忽略的重要记忆
## 示例
\`\`\`json
// 基本用法
{
"fileCount": 5
}
// 全面探索
{
"fileCount": 15
}
\`\`\`
## 调用流程
\`\`\`mermaid
graph TD
A[记忆探索] --> B[getMemoryHints]
B --> C[获取核心节点列表]
C --> D[按权重排序]
D --> E[getMemory加载内容]
\`\`\``,
inputSchema: {
type: "object",
properties: {
fileCount: { type: "number", description: "返回数量", default: 10 }
}
}
},
getBacklinks: {
description: `## 概述
查找引用特定记忆的所有反向链接
## 使用场景
1. 了解某个记忆的重要性
2. 删除记忆前检查依赖
3. 探索知识关联网络
## 示例
\`\`\`json
{
"fragmentName": "主人/2965969512/茶偏好"
}
\`\`\`
## 调用流程
\`\`\`mermaid
graph LR
A[关注特定记忆] --> B[getBacklinks]
B --> C[获取引用列表]
C --> D[分析关联强度]
\`\`\``,
inputSchema: {
type: "object",
properties: {
fragmentName: { type: "string", description: "目标片段名称" }
},
required: ["fragmentName"]
}
},
renameMemory: {
description: `## 概述
重组记忆结构(重命名或合并)。编辑前需要确保在上下文中存在该片段的最新内容。
## 使用场景
1. 优化记忆命名空间
2. 合并相似记忆片段
3. 修复错误命名
## 示例
\`\`\`json
// 简单重命名
{
"sourceFragmentName": "茶偏好",
"targetFragmentName": "主人/饮品偏好"
}
// 合并片段
{
"sourceFragmentName": "绿茶偏好",
"targetFragmentName": "主人/饮品偏好"
}
\`\`\`
## 调用流程
\`\`\`mermaid
graph TB
A[需要重组记忆] --> B{操作类型?}
B -->|重命名| C[renameMemory]
B -->|合并| D[renameMemory合并]
C --> E[更新所有引用]
D --> E
\`\`\``,
inputSchema: {
type: "object",
properties: {
sourceFragmentName: { type: "string", description: "原名称" },
targetFragmentName: { type: "string", description: "新名称" },
conversationId: { type: "string", description: "对话ID,用于区分不同对话实例的访问控制" }
},
required: ["sourceFragmentName", "targetFragmentName", "conversationId"]
}
},
getOptimizeSuggestions: {
description: `## 概述
获取记忆网络优化建议。信息散度 = 权重 / 字符数,用于识别低质量记忆片段。
## 使用场景
1. 定期知识库维护
2. 发现低质量记忆片段
3. 优化信息存储结构
## 示例
\`\`\`json
// 标准分析
{
"optimizationThreshold": 0.1,
"maxFileCount": 10
}
// 深度分析
{
"optimizationThreshold": 0.05,
"maxFileCount": 20
}
\`\`\`
## 调用流程
\`\`\`mermaid
graph TD
A[优化记忆网络] --> B[getOptimizeSuggestions]
B --> C[识别问题片段]
C --> D{问题类型?}
D -->|低信息密度| E[extractMemory拆分]
D -->|孤立片段| F[insertLinkAt关联]
\`\`\``,
inputSchema: {
type: "object",
properties: {
optimizationThreshold: { type: "number", description: "优化阈值", default: 0.1 },
maxFileCount: { type: "number", description: "最大返回数量", default: 10 }
}
}
}
};
//# sourceMappingURL=toolDefinetion.js.map