import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import type { XiaomiNoteClient } from "../client";
import { NotesCache } from "../cache";
import type { WriteFolderEntry } from "../types";
export function registerUpdateFolderTool(server: McpServer, client: XiaomiNoteClient, cache: NotesCache): void {
server.registerTool(
"update_folder",
{
description: "更新文件夹名称或属性",
inputSchema: {
id: z.string().min(1, "文件夹 ID 不能为空"),
subject: z.string().min(1, "文件夹名称不能为空"),
},
},
async ({ id, subject }: { id: string; subject: string }) => {
const folder = cache.getFolders().find((item) => item.id === id) ?? (await cacheRefreshAndFind(id, cache));
if (!folder) {
throw new Error(`未找到文件夹:${id}`);
}
const entry: WriteFolderEntry = {
...folder,
subject,
modifyDate: Date.now(),
};
await client.updateFolder(id, entry);
await cache.refresh();
await server.server.sendResourceUpdated({ uri: "minote://folders" });
await server.server.sendResourceUpdated({ uri: `minote://folders/${id}/notes` });
return {
content: [
{
type: "text" as const,
text: `文件夹 ${id} 更新成功。`,
},
],
};
},
);
}
async function cacheRefreshAndFind(id: string, cache: NotesCache) {
await cache.refresh();
return cache.getFolders().find((item) => item.id === id);
}