import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { NotesCache } from "../cache";
export function registerSearchNotesTool(server: McpServer, cache: NotesCache): void {
server.registerTool(
"search_notes",
{
description: "根据关键词搜索笔记标题和摘要",
inputSchema: {
keyword: z.string().min(1, "搜索关键词不能为空"),
limit: z.number().int().positive().max(100).optional().describe("返回数量限制,默认 20"),
},
},
async ({ keyword, limit = 20 }: { keyword: string; limit?: number }) => {
const matches = cache.searchNotes(keyword).slice(0, limit);
if (matches.length === 0) {
return {
content: [
{
type: "text" as const,
text: "未找到匹配的笔记。",
},
],
};
}
const lines = matches.map((match, index) => {
const time = new Date(match.modifyDate).toISOString();
return `${index + 1}. [${match.id}] ${match.title} (${time})`;
});
return {
content: [
{
type: "text" as const,
text: lines.join("\n"),
},
],
};
},
);
}