* harmonyos_get_document — 按路径获取完整文档内容
*/
import { z } from 'zod';
import { getDocument } from '../lib/knowledge.js';
import { guardKnowledge } from './setup.js';
const GetDocInputSchema = z.object({
path: z.string()
.min(1, '文档路径不能为空')
.describe('文档的相对路径,从 harmonyos_search_docs 或 INDEX.md 中获取。例如 "harmonyos-guides/应用框架/Ability_Kit(程序框架服务)/Stage模型开发指导/Stage模型应用组件/UIAbility组件/UIAbility组件生命周期.md"'),
}).strict();
export function registerGetDocTool(server) {
server.registerTool('harmonyos_get_document', {
title: 'Get HarmonyOS Document',
description: `获取指定文档的完整 Markdown 内容。
根据 harmonyos_search_docs 返回的路径或 INDEX.md 中的路径,读取并返回完整的 Markdown 文档内容。
**使用场景:**
- 查看搜索结果中某篇文档的完整内容
- 阅读 API 参考文档的详细说明
- 获取开发指南的完整步骤
**参数:**
- path: 文档相对路径(必须以 .md 结尾,不能包含 .. 路径穿越)
**返回:** 文档的完整 Markdown 内容,包含标题和正文。`,
inputSchema: GetDocInputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
},
}, async (params) => {
const guard = guardKnowledge();
if (guard)
return { content: [{ type: 'text', text: guard }] };
if (params.path.includes('..')) {
return {
content: [{
type: 'text',
text: '❌ 无效路径:路径不能包含 ".."',
}],
};
}
if (!params.path.endsWith('.md')) {
return {
content: [{
type: 'text',
text: '❌ 无效路径:必须以 .md 结尾',
}],
};
}
const doc = getDocument(params.path);
if (!doc) {
return {
content: [{
type: 'text',
text: `❌ 文档不存在: "${params.path}"\n\n请确认路径是否正确。可使用 harmonyos_search_docs 或 harmonyos_search_index 查找正确的路径。`,
}],
};
}
return {
content: [{
type: 'text',
text: doc.content,
}],
};
});
}