UNPKG

it-tools-mcp

Version:

MCP-compliant server access to over 100 IT tools and utilities commonly used by developers, system administrators, and IT professionals.

61 lines (58 loc) 2.21 kB
import { z } from "zod"; export function registerGenerateMarkdownToc(server) { server.registerTool("generate_markdown_toc", { inputSchema: { markdown: z.string().describe("Markdown content to generate TOC from"), maxLevel: z.number().optional().default(6).describe("Maximum header level to include (1-6)"), generateAnchors: z.boolean().optional().default(true).describe("Whether to generate anchor links") }, // VS Code compliance annotations annotations: { title: "Generate Markdown Toc", readOnlyHint: false } }, async ({ markdown, maxLevel, generateAnchors }) => { // Extract headers from markdown const headerRegex = /^(#{1,6})\s+(.+)$/gm; const headers = []; let match; while ((match = headerRegex.exec(markdown)) !== null) { const level = match[1].length; if (level <= maxLevel) { const text = match[2].trim(); // Generate anchor from text const anchor = text .toLowerCase() .replace(/[^\w\s-]/g, '') .replace(/\s+/g, '-') .replace(/-+/g, '-') .replace(/^-|-$/g, ''); headers.push({ level, text, anchor }); } } if (headers.length === 0) { return { content: [{ type: "text", text: "No headers found in the markdown content." }] }; } // Generate TOC const tocLines = []; headers.forEach(header => { const indent = ' '.repeat(header.level - 1); const link = generateAnchors ? `[${header.text}](#${header.anchor})` : header.text; tocLines.push(`${indent}- ${link}`); }); const toc = tocLines.join('\n'); return { content: [{ type: "text", text: `Table of Contents: ${toc} ${generateAnchors ? '\nAnchors have been generated based on header text.' : ''}` }] }; }); }