| 1 | using System.Text; |
| 2 | using System.Text.Encodings.Web; |
| 3 | using System.Text.Json; |
| 4 | using System.Text.Json.Serialization; |
| 5 | using AIGuiders.SampleMcp; |
| 6 | using McpToolManifest; |
| 7 | |
| 8 | // Regenerates mcp-tools.manifest.json and docs/MCP-TOOLS.md from ToolCatalog. |
| 9 | // JSON on stdout: dotnet run --project tools/ExportMcpManifest |
| 10 | // MD only stdout: dotnet run --project tools/ExportMcpManifest -- --md-only |
| 11 | // Write both files (cwd = MCP root): dotnet run --project tools/ExportMcpManifest -- --write |
| 12 | |
| 13 | var tools = ToolCatalog.Build(); |
| 14 | var cliArgs = Environment.GetCommandLineArgs().Skip(1).ToArray(); |
| 15 | |
| 16 | if (cliArgs.Contains("--write", StringComparer.Ordinal)) |
| 17 | { |
| 18 | var root = Directory.GetCurrentDirectory(); |
| 19 | var jsonPath = Path.Combine(root, "mcp-tools.manifest.json"); |
| 20 | var mdDir = Path.Combine(root, "docs"); |
| 21 | var mdPath = Path.Combine(mdDir, "MCP-TOOLS.md"); |
| 22 | Directory.CreateDirectory(mdDir); |
| 23 | |
| 24 | WriteJsonFile(jsonPath, tools); |
| 25 | var md = McpToolsDocMarkdown.Build(tools.Select(t => (t.Name!, t.Description!))); |
| 26 | File.WriteAllText(mdPath, md, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); |
| 27 | |
| 28 | Console.Error.WriteLine($"Wrote {jsonPath}"); |
| 29 | Console.Error.WriteLine($"Wrote {mdPath}"); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | if (cliArgs.Contains("--md-only", StringComparer.Ordinal)) |
| 34 | { |
| 35 | Console.WriteLine(McpToolsDocMarkdown.Build(tools.Select(t => (t.Name!, t.Description!))).TrimEnd()); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | var doc = new McpToolManifestDocument |
| 40 | { |
| 41 | SchemaVersion = 1, |
| 42 | McpId = "sample-mcp", |
| 43 | Title = "Sample MCP", |
| 44 | Tools = tools.Select(t => new McpToolManifestTool { Name = t.Name!, Description = t.Description }).ToList() |
| 45 | }; |
| 46 | |
| 47 | Console.WriteLine(JsonSerializer.Serialize(doc, new JsonSerializerOptions |
| 48 | { |
| 49 | WriteIndented = true, |
| 50 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 51 | Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 52 | })); |
| 53 | |
| 54 | static void WriteJsonFile(string path, List<ModelContextProtocol.Protocol.Tool> tools) |
| 55 | { |
| 56 | var doc = new McpToolManifestDocument |
| 57 | { |
| 58 | SchemaVersion = 1, |
| 59 | McpId = "sample-mcp", |
| 60 | Title = "Sample MCP", |
| 61 | Tools = tools.Select(t => new McpToolManifestTool { Name = t.Name!, Description = t.Description }).ToList() |
| 62 | }; |
| 63 | |
| 64 | File.WriteAllText( |
| 65 | path, |
| 66 | JsonSerializer.Serialize(doc, new JsonSerializerOptions |
| 67 | { |
| 68 | WriteIndented = true, |
| 69 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 70 | Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 71 | }) + Environment.NewLine, |
| 72 | new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); |
| 73 | } |
| 74 | |
| 75 | file static class McpToolsDocMarkdown |
| 76 | { |
| 77 | public static string Build(IEnumerable<(string Name, string Description)> tools) |
| 78 | { |
| 79 | var sb = new StringBuilder(); |
| 80 | sb.AppendLine("# MCP — tools"); |
| 81 | sb.AppendLine(); |
| 82 | sb.AppendLine("<!-- GENERATED:ToolCatalog START -->"); |
| 83 | sb.AppendLine(); |
| 84 | sb.AppendLine("> Autogenerated from `ToolCatalog.Build()`. Do not edit this block manually."); |
| 85 | sb.AppendLine(); |
| 86 | foreach (var (name, description) in tools) |
| 87 | { |
| 88 | sb.AppendLine($"### `{name}`"); |
| 89 | sb.AppendLine(); |
| 90 | sb.AppendLine(description.TrimEnd()); |
| 91 | sb.AppendLine(); |
| 92 | } |
| 93 | sb.AppendLine("<!-- GENERATED:ToolCatalog END -->"); |
| 94 | sb.AppendLine(); |
| 95 | return sb.ToString(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |