|
| 1 | +/** |
| 2 | + * This script generates tool documentation and updates: |
| 3 | + * - README.md tools list |
| 4 | + * |
| 5 | + * It uses the AllTools array from the tools module. |
| 6 | + */ |
| 7 | + |
| 8 | +import { readFileSync, writeFileSync } from "fs"; |
| 9 | +import { join, dirname } from "path"; |
| 10 | +import { fileURLToPath } from "url"; |
| 11 | +import { AllTools } from "../../src/tools/index.js"; |
| 12 | + |
| 13 | +const __filename = fileURLToPath(import.meta.url); |
| 14 | +const __dirname = dirname(__filename); |
| 15 | + |
| 16 | +interface ToolInfo { |
| 17 | + name: string; |
| 18 | + description: string; |
| 19 | + category: string; |
| 20 | + operationType: string; |
| 21 | +} |
| 22 | + |
| 23 | +const overrides: Record<string, string> = { |
| 24 | + connect: "Connect to a MongoDB instance", |
| 25 | + "switch-connection": "Switch to a different MongoDB connection", |
| 26 | +}; |
| 27 | + |
| 28 | +function extractToolInformation(): ToolInfo[] { |
| 29 | + const tools: ToolInfo[] = []; |
| 30 | + |
| 31 | + for (const ToolClass of AllTools) { |
| 32 | + // Create a minimal instance to access instance properties |
| 33 | + // We need to provide dummy params since we only need name and description |
| 34 | + const dummyParams = { |
| 35 | + category: ToolClass.category, |
| 36 | + operationType: ToolClass.operationType, |
| 37 | + session: { |
| 38 | + on: () => {}, |
| 39 | + off: () => {}, |
| 40 | + emit: () => false, |
| 41 | + connectionManager: null, |
| 42 | + } as never, |
| 43 | + config: { |
| 44 | + previewFeatures: [], |
| 45 | + vectorSearchDimensions: 1024, |
| 46 | + vectorSearchModel: "voyage-3-large", |
| 47 | + readOnly: false, |
| 48 | + disabledTools: [], |
| 49 | + } as never, |
| 50 | + telemetry: { |
| 51 | + emitEvents: () => {}, |
| 52 | + } as never, |
| 53 | + elicitation: { |
| 54 | + requestConfirmation: () => Promise.resolve(false), |
| 55 | + } as never, |
| 56 | + }; |
| 57 | + |
| 58 | + try { |
| 59 | + const instance = new ToolClass(dummyParams); |
| 60 | + // Access the protected description property |
| 61 | + const description = |
| 62 | + (instance as unknown as { description?: string }).description || "No description available"; |
| 63 | + tools.push({ |
| 64 | + name: instance.name, |
| 65 | + description: overrides[instance.name] || description, |
| 66 | + category: ToolClass.category, |
| 67 | + operationType: ToolClass.operationType, |
| 68 | + }); |
| 69 | + } catch (error) { |
| 70 | + console.error(`Error instantiating tool ${ToolClass.name}:`, error); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Sort by category first, then by name |
| 75 | + return tools.sort((a, b) => { |
| 76 | + if (a.category !== b.category) { |
| 77 | + return a.category.localeCompare(b.category); |
| 78 | + } |
| 79 | + return a.name.localeCompare(b.name); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +function generateReadmeToolsList(tools: ToolInfo[]): string { |
| 84 | + const sections: string[] = []; |
| 85 | + |
| 86 | + // Group tools by category |
| 87 | + const toolsByCategory: Record<string, ToolInfo[]> = {}; |
| 88 | + for (const tool of tools) { |
| 89 | + if (!toolsByCategory[tool.category]) { |
| 90 | + toolsByCategory[tool.category] = []; |
| 91 | + } |
| 92 | + const categoryTools = toolsByCategory[tool.category]; |
| 93 | + if (categoryTools) { |
| 94 | + categoryTools.push(tool); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Generate sections for each category |
| 99 | + const categoryTitles: Record<string, string> = { |
| 100 | + atlas: "MongoDB Atlas Tools", |
| 101 | + "atlas-local": "MongoDB Atlas Local Tools", |
| 102 | + mongodb: "MongoDB Database Tools", |
| 103 | + }; |
| 104 | + |
| 105 | + const categoryOrder = ["atlas", "atlas-local", "mongodb"]; |
| 106 | + |
| 107 | + for (const category of categoryOrder) { |
| 108 | + if (!toolsByCategory[category]) continue; |
| 109 | + |
| 110 | + sections.push(`#### ${categoryTitles[category]}\n`); |
| 111 | + |
| 112 | + for (const tool of toolsByCategory[category]) { |
| 113 | + sections.push(`- \`${tool.name}\` - ${tool.description}`); |
| 114 | + } |
| 115 | + |
| 116 | + // Add note for Atlas tools |
| 117 | + if (category === "atlas") { |
| 118 | + sections.push( |
| 119 | + "\nNOTE: atlas tools are only available when you set credentials on [configuration](#configuration) section.\n" |
| 120 | + ); |
| 121 | + } else { |
| 122 | + sections.push(""); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return sections.join("\n"); |
| 127 | +} |
| 128 | + |
| 129 | +function updateReadmeToolsList(tools: ToolInfo[]): void { |
| 130 | + const readmePath = join(__dirname, "..", "..", "README.md"); |
| 131 | + let content = readFileSync(readmePath, "utf-8"); |
| 132 | + |
| 133 | + const newToolsList = generateReadmeToolsList(tools); |
| 134 | + |
| 135 | + // Find and replace the tools list section |
| 136 | + // Match from "### Tool List" to the next "## " section |
| 137 | + const toolsRegex = /### Tool List\n\n([\s\S]*?)\n\n## 📄 Supported Resources/; |
| 138 | + const replacement = `### Tool List\n\n${newToolsList}\n## 📄 Supported Resources`; |
| 139 | + |
| 140 | + content = content.replace(toolsRegex, replacement); |
| 141 | + |
| 142 | + writeFileSync(readmePath, content, "utf-8"); |
| 143 | + console.log("✓ Updated README.md tools list"); |
| 144 | +} |
| 145 | + |
| 146 | +export function generateToolDocumentation(): void { |
| 147 | + const toolInfo = extractToolInformation(); |
| 148 | + updateReadmeToolsList(toolInfo); |
| 149 | +} |
0 commit comments