Skip to content

Commit 76288c9

Browse files
committed
Enhance McpDocumentTransformer to support JSON-RPC request and response schemas
1 parent d01396a commit 76288c9

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

shared/McpSamples.Shared/OpenApi/McpDocumentTransformer.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net.Mime;
12
using System.Text.Json.Nodes;
23

34
using McpSamples.Shared.Configurations;
@@ -6,6 +7,8 @@
67
using Microsoft.AspNetCore.OpenApi;
78
using Microsoft.OpenApi;
89

10+
using ModelContextProtocol.Protocol;
11+
912
namespace McpSamples.Shared.OpenApi;
1013

1114
/// <summary>
@@ -16,7 +19,7 @@ namespace McpSamples.Shared.OpenApi;
1619
public class McpDocumentTransformer<T>(T appsettings, IHttpContextAccessor accessor) : IOpenApiDocumentTransformer where T : AppSettings, new()
1720
{
1821
/// <inheritdoc />
19-
public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
22+
public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
2023
{
2124
document.Info = new OpenApiInfo
2225
{
@@ -33,6 +36,15 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC
3336
: "http://localhost:8080/"
3437
}
3538
];
39+
40+
var jsonRpcResponse = await context.GetOrCreateSchemaAsync(typeof(JsonRpcResponse), cancellationToken: cancellationToken);
41+
42+
var jsonRpcRequest = await context.GetOrCreateSchemaAsync(typeof(JsonRpcRequest), cancellationToken: cancellationToken);
43+
44+
document.AddComponent(nameof(JsonRpcResponse), jsonRpcResponse);
45+
46+
document.AddComponent(nameof(JsonRpcRequest), jsonRpcRequest);
47+
3648
var pathItem = new OpenApiPathItem();
3749
pathItem.AddOperation(HttpMethod.Post, new OpenApiOperation
3850
{
@@ -47,13 +59,29 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC
4759
["200"] = new OpenApiResponse
4860
{
4961
Description = "Success",
62+
Content = new Dictionary<string, OpenApiMediaType>
63+
{
64+
[MediaTypeNames.Application.Json] = new()
65+
{
66+
Schema = new OpenApiSchemaReference(nameof(JsonRpcResponse), document),
67+
},
68+
},
5069
}
51-
}
70+
},
71+
RequestBody = new OpenApiRequestBody
72+
{
73+
Required = true,
74+
Content = new Dictionary<string, OpenApiMediaType>
75+
{
76+
[MediaTypeNames.Application.Json] = new()
77+
{
78+
Schema = new OpenApiSchemaReference(nameof(JsonRpcRequest), document),
79+
},
80+
},
81+
},
5282
});
5383

5484
document.Paths ??= [];
5585
document.Paths.Add("/mcp", pathItem);
56-
57-
return Task.CompletedTask;
5886
}
5987
}

0 commit comments

Comments
 (0)