-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathMcpServerGenerator.cs
More file actions
60 lines (53 loc) · 2.13 KB
/
McpServerGenerator.cs
File metadata and controls
60 lines (53 loc) · 2.13 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
#pragma warning disable CS8509
using Outcome;
namespace RestClient.Net.McpGenerator;
/// <summary>Generates MCP server code from OpenAPI specifications.</summary>
public static class McpServerGenerator
{
/// <summary>Generates MCP server tools code from an OpenAPI document.</summary>
/// <param name="openApiContent">The OpenAPI document content (JSON or YAML).</param>
/// <param name="namespace">The namespace for generated MCP tools.</param>
/// <param name="serverName">The MCP server name.</param>
/// <param name="extensionsNamespace">The namespace of the pre-generated extensions.</param>
/// <param name="includeTags">Optional set of tags to include. If specified, only operations with these tags are generated.</param>
/// <returns>A Result containing the generated C# code or error message.</returns>
#pragma warning disable CA1054
public static Result<string, string> Generate(
string openApiContent,
string @namespace,
string serverName,
string extensionsNamespace,
ISet<string>? includeTags = null
)
#pragma warning restore CA1054
{
try
{
var settings = new OpenApiReaderSettings();
settings.AddYamlReader();
var readResult = OpenApiDocument.Parse(openApiContent, settings: settings);
if (readResult.Document == null)
{
return new Result<string, string>.Error<string, string>(
"Error parsing OpenAPI: Document is null"
);
}
var document = readResult.Document;
return new Result<string, string>.Ok<string, string>(
McpToolGenerator.GenerateTools(
document,
@namespace,
serverName,
extensionsNamespace,
includeTags
)
);
}
catch (Exception ex)
{
return new Result<string, string>.Error<string, string>(
$"Exception during code generation: {ex.GetType().Name}: {ex.Message}\n{ex.StackTrace}"
);
}
}
}