zettelkasten-memory-server
Version:
基于 Zettelkasten 记忆片段盒方法的 MCP 记忆服务器 v1.6.X
92 lines (88 loc) • 3.75 kB
JavaScript
/**
* 资源处理器 - 处理 Zettelkasten 记忆片段资源的列出和读取
*/
/**
* 列出所有 Zettelkasten 记忆片段作为资源
*/
export async function listZettelkastenResources(zettelkastenManager) {
try {
// 只返回一个示范资源,说明如何使用 memory:// 协议访问任意记忆片段
return {
resources: [
{
uri: "memory:///",
mimeType: "text/plain",
name: "Zettelkasten 记忆片段访问",
description: "使用 memory:///记忆片段名 访问任意记忆片段,可添加 #depth 指定展开深度(如 memory:///JavaScript#1)"
}
]
};
}
catch (error) {
console.error('列出 Zettelkasten 资源时发生错误:', error);
return {
resources: []
};
}
}
/**
* 读取特定 Zettelkasten 记忆片段的内容
* 支持通过 URI fragment 指定展开深度,如: memory:///fragmentName#2
*/
export async function readZettelkastenResource(zettelkastenManager, uri) {
try {
const url = new URL(uri);
const fragmentName = decodeURIComponent(url.pathname.replace(/^\//, ''));
// 如果是根路径,提供使用帮助
if (!fragmentName) {
const hints = await zettelkastenManager.getMemoryHints(10);
const exampleFragments = hints.fragmentNames.slice(0, 3);
return {
contents: [{
uri: uri,
mimeType: "text/plain",
text: `# 📚 Zettelkasten 记忆片段访问指南
## 🎯 如何访问记忆片段
使用以下格式访问任意记忆片段:
- \`memory:///记忆片段名\` - 获取记忆片段基础内容
- \`memory:///记忆片段名#1\` - 展开一层引用
- \`memory:///记忆片段名#2\` - 展开两层引用
## 📝 当前可用记忆片段${exampleFragments.length > 0 ? '(示例)' : ''}
${exampleFragments.length > 0 ?
exampleFragments.map(name => `- \`memory:///${encodeURIComponent(name)}\` → ${name}`).join('\n') :
'目前还没有记忆片段,请使用 setMemory 工具创建第一张记忆片段'}
## 💡 提示
- 可以访问任何存在的记忆片段,即使它不在上述示例中
- 使用 getMemoryHints 工具查看所有可用记忆片段
- 记忆片段名支持中文和特殊字符`
}]
};
}
// 从 fragment 中获取展开深度,默认为 0
let expandDepth = 0;
if (url.hash) {
const hashValue = url.hash.substring(1); // 移除 # 字符
const parsedDepth = parseInt(hashValue, 10);
if (!isNaN(parsedDepth) && parsedDepth >= 0) {
expandDepth = parsedDepth;
}
}
// 调用 getMemory 方法获取记忆片段内容
const content = await zettelkastenManager.getMemory(fragmentName, expandDepth);
if (!content) {
throw new Error(`记忆片段 "${fragmentName}" 未找到`);
}
return {
contents: [{
uri: uri,
mimeType: "text/plain",
text: expandDepth > 0 ? `📄 **${fragmentName}** (展开深度: ${expandDepth})\n\n${content}` : `📄 **${fragmentName}**\n\n${content}`
}]
};
}
catch (error) {
console.error('读取 Zettelkasten 资源时发生错误:', error);
throw new Error(`无法读取记忆片段资源: ${error instanceof Error ? error.message : String(error)}`);
}
}
//# sourceMappingURL=resourceHandlers.js.map