| 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 McpToolManifest; |
| 7 | using Tool = ModelContextProtocol.Protocol.Tool; |
| 8 | |
| 9 | var tools = ToolCatalog.Build(); |
| 10 | var cliArgs = Environment.GetCommandLineArgs().Skip(1).ToArray(); |
| 11 | |
| 12 | if (cliArgs.Contains("--write", StringComparer.Ordinal)) |
| 13 | { |
| 14 | var root = Directory.GetCurrentDirectory(); |
| 15 | var jsonPath = Path.Combine(root, "mcp-tools.manifest.json"); |
| 16 | var mdDir = Path.Combine(root, "docs"); |
| 17 | var mdPath = Path.Combine(mdDir, "MCP-TOOLS.md"); |
| 18 | Directory.CreateDirectory(mdDir); |
| 19 | |
| 20 | WriteJsonFile(jsonPath, tools); |
| 21 | var md = McpToolsDocMarkdown.Build(tools.Select(t => (t.Name!, t.Description!))); |
| 22 | File.WriteAllText(mdPath, md, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); |
| 23 | |
| 24 | Console.Error.WriteLine($"Wrote {jsonPath}"); |
| 25 | Console.Error.WriteLine($"Wrote {mdPath}"); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | if (cliArgs.Contains("--md-only", StringComparer.Ordinal)) |
| 30 | { |
| 31 | Console.WriteLine(McpToolsDocMarkdown.Build(tools.Select(t => (t.Name!, t.Description!))).TrimEnd()); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | var doc = new McpToolManifestDocument |
| 36 | { |
| 37 | SchemaVersion = 1, |
| 38 | McpId = "git-mcp", |
| 39 | Title = "Git MCP", |
| 40 | Tools = tools.Select(t => new McpToolManifestTool { Name = t.Name, Description = t.Description }).ToList() |
| 41 | }; |
| 42 | |
| 43 | Console.WriteLine(JsonSerializer.Serialize(doc, new JsonSerializerOptions |
| 44 | { |
| 45 | WriteIndented = true, |
| 46 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 47 | Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 48 | })); |
| 49 | |
| 50 | static void WriteJsonFile(string path, List<Tool> tools) |
| 51 | { |
| 52 | var doc = new McpToolManifestDocument |
| 53 | { |
| 54 | SchemaVersion = 1, |
| 55 | McpId = "git-mcp", |
| 56 | Title = "Git MCP", |
| 57 | Tools = tools.Select(t => new McpToolManifestTool { Name = t.Name, Description = t.Description }).ToList() |
| 58 | }; |
| 59 | |
| 60 | File.WriteAllText( |
| 61 | path, |
| 62 | JsonSerializer.Serialize(doc, new JsonSerializerOptions |
| 63 | { |
| 64 | WriteIndented = true, |
| 65 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 66 | Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 67 | }) + Environment.NewLine, |
| 68 | new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); |
| 69 | } |
| 70 | |