UNPKG

@sofianedjerbi/knowledge-tree-mcp

Version:

MCP server for hierarchical project knowledge management

64 lines 2.15 kB
/** * Export knowledge tool implementation * Exports knowledge base to various formats for documentation */ import { join } from 'path'; import { EXPORT_DEFAULTS } from '../constants/index.js'; import { readFile, exportToMarkdown, exportToHTML } from '../utils/index.js'; /** * Handler for the export_knowledge tool */ export const exportKnowledgeHandler = async (args, context) => { const { format = EXPORT_DEFAULTS.FORMAT, filter = {}, include_links = EXPORT_DEFAULTS.INCLUDE_LINKS } = args; // Get all entries const allEntries = await context.scanKnowledgeTree(); let entries = []; // Load and filter entries for (const path of allEntries) { const fullPath = join(context.knowledgeRoot, path); try { const content = await readFile(fullPath); const entry = JSON.parse(content); // Apply filters if (filter.priority && filter.priority.length > 0) { if (!filter.priority.includes(entry.priority)) continue; } if (filter.category) { if (!path.toLowerCase().includes(filter.category.toLowerCase())) continue; } entries.push({ path, entry }); } catch (error) { // Skip invalid entries } } // Sort by priority and path const priorityOrder = { "CRITICAL": 0, "REQUIRED": 1, "COMMON": 2, "EDGE-CASE": 3 }; entries.sort((a, b) => { const priorityDiff = priorityOrder[a.entry.priority] - priorityOrder[b.entry.priority]; if (priorityDiff !== 0) return priorityDiff; return a.path.localeCompare(b.path); }); let output = ""; switch (format) { case "html": output = exportToHTML(entries, include_links); break; case "markdown": default: output = exportToMarkdown(entries, include_links); break; } return { content: [ { type: "text", text: output, }, ], }; }; //# sourceMappingURL=export.js.map