-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathProgram.cs
More file actions
216 lines (191 loc) · 6.57 KB
/
Program.cs
File metadata and controls
216 lines (191 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#pragma warning disable CA1502
using RestClient.Net.McpGenerator;
if (args.Length == 0 || args.Contains("--help") || args.Contains("-h"))
{
PrintUsage();
return 0;
}
var config = ParseArgs(args);
if (config is null)
{
return 1;
}
await GenerateCode(config).ConfigureAwait(false);
return 0;
static void PrintUsage()
{
Console.WriteLine("RestClient.Net MCP Server Generator");
Console.WriteLine("====================================\n");
Console.WriteLine("Generates MCP tool code that wraps RestClient.Net extension methods.\n");
Console.WriteLine("Usage:");
Console.WriteLine(" mcp-generator [options]\n");
Console.WriteLine("Options:");
Console.WriteLine(
" -u, --openapi-url <url> (Required) URL or file path to OpenAPI spec"
);
Console.WriteLine(
" -o, --output-file <path> (Required) Output file path for generated code"
);
Console.WriteLine(
" -n, --namespace <namespace> MCP server namespace (default: 'McpServer')"
);
Console.WriteLine(" -s, --server-name <name> MCP server name (default: 'ApiMcp')");
Console.WriteLine(
" --ext-namespace <namespace> Extensions namespace (default: 'Generated')"
);
Console.WriteLine(
" --ext-class <class> Extensions class name (default: 'ApiExtensions')"
);
Console.WriteLine(
" -t, --tags <tag1,tag2> Comma-separated list of OpenAPI tags to include (optional)"
);
Console.WriteLine(" -h, --help Show this help message");
}
static Config? ParseArgs(string[] args)
{
string? openApiUrl = null;
string? outputFile = null;
var namespaceName = "McpServer";
var serverName = "ApiMcp";
var extensionsNamespace = "Generated";
var extensionsClass = "ApiExtensions";
string? tagsFilter = null;
for (var i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "-u"
or "--openapi-url":
openApiUrl = GetNextArg(args, i++, "openapi-url");
break;
case "-o"
or "--output-file":
outputFile = GetNextArg(args, i++, "output-file");
break;
case "-n"
or "--namespace":
namespaceName = GetNextArg(args, i++, "namespace") ?? namespaceName;
break;
case "-s"
or "--server-name":
serverName = GetNextArg(args, i++, "server-name") ?? serverName;
break;
case "--ext-namespace":
extensionsNamespace = GetNextArg(args, i++, "ext-namespace") ?? extensionsNamespace;
break;
case "--ext-class":
extensionsClass = GetNextArg(args, i++, "ext-class") ?? extensionsClass;
break;
case "-t"
or "--tags":
tagsFilter = GetNextArg(args, i++, "tags");
break;
default:
break;
}
}
if (string.IsNullOrEmpty(openApiUrl))
{
Console.WriteLine("Error: --openapi-url is required");
PrintUsage();
return null;
}
if (string.IsNullOrEmpty(outputFile))
{
Console.WriteLine("Error: --output-file is required");
PrintUsage();
return null;
}
return new Config(
openApiUrl,
outputFile,
namespaceName,
serverName,
extensionsNamespace,
extensionsClass,
tagsFilter
);
}
static string? GetNextArg(string[] args, int currentIndex, string optionName)
{
if (currentIndex + 1 >= args.Length)
{
Console.WriteLine($"Error: --{optionName} requires a value");
return null;
}
return args[currentIndex + 1];
}
static async Task GenerateCode(Config config)
{
Console.WriteLine("RestClient.Net MCP Server Generator");
Console.WriteLine("====================================\n");
string openApiSpec;
var isUrl =
config.OpenApiUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| config.OpenApiUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
if (!isUrl)
{
var filePath = config.OpenApiUrl.StartsWith("file://", StringComparison.OrdinalIgnoreCase)
? config.OpenApiUrl[7..]
: config.OpenApiUrl;
Console.WriteLine($"Reading OpenAPI spec from file: {filePath}");
if (!File.Exists(filePath))
{
Console.WriteLine($"Error: File not found: {filePath}");
return;
}
openApiSpec = await File.ReadAllTextAsync(filePath).ConfigureAwait(false);
}
else
{
Console.WriteLine($"Downloading OpenAPI spec from: {config.OpenApiUrl}");
using var httpClient = new HttpClient();
openApiSpec = await httpClient.GetStringAsync(config.OpenApiUrl).ConfigureAwait(false);
}
Console.WriteLine($"Read {openApiSpec.Length} characters\n");
// Parse tags filter if provided
ISet<string>? includeTags = null;
if (!string.IsNullOrWhiteSpace(config.TagsFilter))
{
includeTags = new HashSet<string>(
config.TagsFilter.Split(
',',
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries
),
StringComparer.OrdinalIgnoreCase
);
Console.WriteLine($"Filtering to tags: {string.Join(", ", includeTags)}");
}
Console.WriteLine("Generating MCP tools code...");
var result = McpServerGenerator.Generate(
openApiSpec,
@namespace: config.Namespace,
serverName: config.ServerName,
extensionsNamespace: config.ExtensionsNamespace,
includeTags: includeTags
);
#pragma warning disable IDE0010
switch (result)
#pragma warning restore IDE0010
{
case Outcome.Result<string, string>.Ok<string, string>(var code):
await File.WriteAllTextAsync(config.OutputFile, code).ConfigureAwait(false);
Console.WriteLine($"Generated {code.Length} characters of MCP tools code");
Console.WriteLine($"\nSaved to: {config.OutputFile}");
Console.WriteLine("\nGeneration completed successfully!");
break;
case Outcome.Result<string, string>.Error<string, string>(var error):
Console.WriteLine("\nCode generation failed:");
Console.WriteLine(error);
break;
}
}
internal sealed record Config(
string OpenApiUrl,
string OutputFile,
string Namespace,
string ServerName,
string ExtensionsNamespace,
string ExtensionsClass,
string? TagsFilter
);