| 1 | using System.Text; |
| 2 | using System.Text.Encodings.Web; |
| 3 | using System.Text.Json; |
| 4 | using System.Text.Json.Serialization; |
| 5 | using ExportMcpManifest; |
| 6 | using HybridCodebaseIndex.Mcp; |
| 7 | using McpToolManifest; |
| 8 | using Tool = ModelContextProtocol.Protocol.Tool; |
| 9 | |
| 10 | // Regenerates mcp-tools.manifest.json and docs/MCP-TOOLS.md from ToolCatalog. |
| 11 | // JSON на stdout: dotnet run --project tools/ExportMcpManifest |
| 12 | // только MD stdout: dotnet run --project tools/ExportMcpManifest -- --md-only |
| 13 | // записать оба файла (cwd = корень hybrid-codebase-index): dotnet run --project tools/ExportMcpManifest -- --write |
| 14 | |
| 15 | var tools = ToolCatalog.Build(); |
| 16 | var cliArgs = Environment.GetCommandLineArgs().Skip(1).ToArray(); |
| 17 | |
| 18 | if (cliArgs.Contains("--write", StringComparer.Ordinal)) |
| 19 | { |
| 20 | var root = Directory.GetCurrentDirectory(); |
| 21 | var jsonPath = Path.Combine(root, "mcp-tools.manifest.json"); |
| 22 | var mdDir = Path.Combine(root, "docs"); |
| 23 | var mdPath = Path.Combine(mdDir, "MCP-TOOLS.md"); |
| 24 | Directory.CreateDirectory(mdDir); |
| 25 | |
| 26 | WriteJsonFile(jsonPath, tools); |
| 27 | var md = McpToolsDocMarkdown.Build(tools.Select(t => (t.Name!, t.Description!))); |
| 28 | File.WriteAllText(mdPath, md, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); |
| 29 | |
| 30 | Console.Error.WriteLine($"Wrote {jsonPath}"); |
| 31 | Console.Error.WriteLine($"Wrote {mdPath}"); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | if (cliArgs.Contains("--md-only", StringComparer.Ordinal)) |
| 36 | { |
| 37 | Console.WriteLine(McpToolsDocMarkdown.Build(tools.Select(t => (t.Name!, t.Description!))).TrimEnd()); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | var doc = new McpToolManifestDocument |
| 42 | { |
| 43 | SchemaVersion = 1, |
| 44 | McpId = "hybrid-codebase-index", |
| 45 | Title = "Hybrid Codebase Index MCP", |
| 46 | Tools = tools.Select(t => new McpToolManifestTool { Name = t.Name, Description = t.Description }).ToList() |
| 47 | }; |
| 48 | |
| 49 | Console.WriteLine(JsonSerializer.Serialize(doc, new JsonSerializerOptions |
| 50 | { |
| 51 | WriteIndented = true, |
| 52 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 53 | Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 54 | })); |
| 55 | |
| 56 | static void WriteJsonFile(string path, List<Tool> tools) |
| 57 | { |
| 58 | var doc = new McpToolManifestDocument |
| 59 | { |
| 60 | SchemaVersion = 1, |
| 61 | McpId = "hybrid-codebase-index", |
| 62 | Title = "Hybrid Codebase Index MCP", |
| 63 | Tools = tools.Select(t => new McpToolManifestTool { Name = t.Name, Description = t.Description }).ToList() |
| 64 | }; |
| 65 | |
| 66 | File.WriteAllText( |
| 67 | path, |
| 68 | JsonSerializer.Serialize(doc, new JsonSerializerOptions |
| 69 | { |
| 70 | WriteIndented = true, |
| 71 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 72 | Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 73 | }) + Environment.NewLine, |
| 74 | new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); |
| 75 | } |
| 76 | |
| 77 | |