Skip to content

Commit 0e70f5c

Browse files
committed
feat: search doc tool
1 parent a3cafd2 commit 0e70f5c

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

mcp/src/tools/rag.ts

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async function prepareKnowledgeBaseWebTemplate() {
6464
const zip = new AdmZip(zipPath);
6565
zip.extractAllTo(extractDir, true);
6666

67-
return collectSkillDescriptions(extractDir);
67+
return collectSkillDescriptions(path.join(extractDir, ".claude", "skills"));
6868
}
6969

7070
export async function registerRagTools(server: ExtendedMcpServer) {
@@ -193,17 +193,38 @@ export async function registerRagTools(server: ExtendedMcpServer) {
193193
"searchKnowledgeBase",
194194
{
195195
title: "云开发知识库检索",
196-
description: "云开发知识库智能检索工具,支持云开发与云函数知识的向量查询",
196+
description: `云开发知识库智能检索工具,支持向量查询 (vector) 和固定文档 (doc) 查询。
197+
198+
强烈推荐始终优先使用固定文档 (doc) 模式进行检索,仅当固定文档无法覆盖你的问题时,再使用向量查询 (vector) 模式。
199+
200+
固定文档 (doc) 查询当前支持 ${skills.length} 个固定文档,分别是:
201+
${skills
202+
.map(
203+
(skill) =>
204+
`文档名:${path.basename(path.dirname(skill.absolutePath))} 文档介绍:${
205+
skill.description
206+
}`,
207+
)
208+
.join("\n")}`,
197209
inputSchema: {
210+
mode: z.enum(["vector", "doc"]),
211+
docName: z
212+
.enum(
213+
skills.map((skill) =>
214+
path.basename(path.dirname(skill.absolutePath)),
215+
) as unknown as [string, ...string[]],
216+
)
217+
.optional()
218+
.describe("mode=doc 时指定。文档名称。"),
198219
threshold: z
199220
.number()
200221
.default(0.5)
201222
.optional()
202-
.describe("相似性检索阈值"),
203-
id: KnowledgeBaseEnum.describe(
204-
"知识库范围,cloudbase=云开发全量知识,scf=云开发的云函数知识, miniprogram=小程序知识(不包含云开发与云函数知识)",
223+
.describe("mode=vector 时指定。相似性检索阈值"),
224+
id: KnowledgeBaseEnum.optional().describe(
225+
"mode=vector 时指定。知识库范围,cloudbase=云开发全量知识,scf=云开发的云函数知识, miniprogram=小程序知识(不包含云开发与云函数知识)",
205226
),
206-
content: z.string().describe("检索内容"),
227+
content: z.string().describe("mode=vector 时指定。检索内容").optional(),
207228
options: z
208229
.object({
209230
chunkExpand: z
@@ -216,12 +237,12 @@ export async function registerRagTools(server: ExtendedMcpServer) {
216237
),
217238
})
218239
.optional()
219-
.describe("其他选项"),
240+
.describe("mode=vector 时指定。其他选项"),
220241
limit: z
221242
.number()
222243
.default(5)
223244
.optional()
224-
.describe("指定返回最相似的 Top K 的 K 的值"),
245+
.describe("mode=vector 时指定。指定返回最相似的 Top K 的 K 的值"),
225246
},
226247
annotations: {
227248
readOnlyHint: true,
@@ -235,13 +256,23 @@ export async function registerRagTools(server: ExtendedMcpServer) {
235256
options: { chunkExpand = [3, 3] } = {},
236257
limit = 5,
237258
threshold = 0.5,
238-
}: {
239-
id: string;
240-
content: string;
241-
options?: { chunkExpand?: number[] };
242-
limit?: number;
243-
threshold?: number;
259+
mode,
260+
docName,
244261
}) => {
262+
if (mode === "doc") {
263+
const absolutePath = skills.find((skill) =>
264+
skill.absolutePath.includes(docName!),
265+
)!.absolutePath;
266+
267+
return {
268+
content: [
269+
{
270+
type: "text",
271+
text: `The doc's absolute path is: ${absolutePath}. ${(await fs.readFile(absolutePath)).toString()}`,
272+
},
273+
],
274+
};
275+
}
245276
// 枚举到后端 id 映射
246277
const backendId =
247278
KnowledgeBaseIdMap[id as keyof typeof KnowledgeBaseIdMap] || id;

0 commit comments

Comments
 (0)