import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import type { ContentConverter } from "../converter";
import type { XiaomiNoteClient } from "../client";
import { NotesCache } from "../cache";
import { buildExtraInfoString, extractSnippetFromXml } from "../note";
import type { WriteNoteEntry } from "../types";
interface UpdateNoteArgs {
id: string;
content: string;
title?: string;
folderId?: string;
colorId?: number;
}
export function registerUpdateNoteTool(
server: McpServer,
client: XiaomiNoteClient,
converter: ContentConverter,
cache: NotesCache,
): void {
server.registerTool(
"update_note",
{
description: "根据 ID 更新笔记内容",
inputSchema: {
id: z.string().min(1, "笔记 ID 不能为空"),
content: z.string().min(1, "笔记内容不能为空"),
title: z.string().optional(),
folderId: z.string().optional(),
colorId: z.number().int().optional(),
},
},
async ({ id, content, title, folderId, colorId }: UpdateNoteArgs) => {
const current = await client.getNote(id);
const note = current.data.entry;
const xmlContent = converter.markdownToXml(content);
const now = Date.now();
const entry: WriteNoteEntry = {
id: note.id,
tag: note.tag,
status: note.status,
createDate: note.createDate,
modifyDate: now,
colorId: colorId ?? note.colorId,
content: xmlContent,
setting: note.setting,
folderId: folderId ?? note.folderId,
alertDate: note.alertDate,
extraInfo: buildExtraInfoString(title, note.extraInfo),
subject: note.subject,
snippet: extractSnippetFromXml(xmlContent),
};
await client.updateNote(id, entry);
const latest = await client.getNote(id);
cache.upsertNote(latest.data.entry);
await server.server.sendResourceUpdated({ uri: `minote://notes/${id}` });
return {
content: [
{
type: "text" as const,
text: `笔记 ${id} 更新完成。`,
},
],
};
},
);
}