@@ -58,6 +58,22 @@ public async Task ConnectAndReceiveMessage_InMemoryServer_WithFullEndpointEventU
58
58
Assert . True ( true ) ;
59
59
}
60
60
61
+ [ Fact ]
62
+ public async Task ConnectAndReceiveMessage_ServerReturningJsonInPostRequest ( )
63
+ {
64
+ await using var app = Builder . Build ( ) ;
65
+ MapAbsoluteEndpointUriMcp ( app , respondInJson : true ) ;
66
+
67
+ await app . StartAsync ( TestContext . Current . CancellationToken ) ;
68
+
69
+ await using var mcpClient = await ConnectMcpClientAsync ( ) ;
70
+
71
+ // Send a test message through POST endpoint
72
+ await mcpClient . SendNotificationAsync ( "test/message" , new Envelope { Message = "Hello, SSE!" } , serializerOptions : JsonContext . Default . Options , cancellationToken : TestContext . Current . CancellationToken ) ;
73
+
74
+ Assert . True ( true ) ;
75
+ }
76
+
61
77
[ Fact ]
62
78
public async Task ConnectAndReceiveNotification_InMemoryServer ( )
63
79
{
@@ -220,7 +236,7 @@ public async Task EmptyAdditionalHeadersKey_Throws_InvalidOperationException()
220
236
Assert . Equal ( "Failed to add header '' with value '' from AdditionalHeaders." , ex . Message ) ;
221
237
}
222
238
223
- private static void MapAbsoluteEndpointUriMcp ( IEndpointRouteBuilder endpoints )
239
+ private static void MapAbsoluteEndpointUriMcp ( IEndpointRouteBuilder endpoints , bool respondInJson = false )
224
240
{
225
241
var loggerFactory = endpoints . ServiceProvider . GetRequiredService < ILoggerFactory > ( ) ;
226
242
var optionsSnapshot = endpoints . ServiceProvider . GetRequiredService < IOptions < McpServerOptions > > ( ) ;
@@ -267,7 +283,7 @@ private static void MapAbsoluteEndpointUriMcp(IEndpointRouteBuilder endpoints)
267
283
await Results . BadRequest ( "Session not started." ) . ExecuteAsync ( context ) ;
268
284
return ;
269
285
}
270
- var message = ( JsonRpcMessage ? ) await context . Request . ReadFromJsonAsync ( McpJsonUtilities . DefaultOptions . GetTypeInfo ( typeof ( JsonRpcMessage ) ) , context . RequestAborted ) ;
286
+ var message = await context . Request . ReadFromJsonAsync < JsonRpcMessage > ( McpJsonUtilities . DefaultOptions , context . RequestAborted ) ;
271
287
if ( message is null )
272
288
{
273
289
await Results . BadRequest ( "No message in request body." ) . ExecuteAsync ( context ) ;
@@ -276,7 +292,15 @@ private static void MapAbsoluteEndpointUriMcp(IEndpointRouteBuilder endpoints)
276
292
277
293
await session . OnMessageReceivedAsync ( message , context . RequestAborted ) ;
278
294
context . Response . StatusCode = StatusCodes . Status202Accepted ;
279
- await context . Response . WriteAsync ( "Accepted" ) ;
295
+
296
+ if ( respondInJson )
297
+ {
298
+ await context . Response . WriteAsJsonAsync ( message , McpJsonUtilities . DefaultOptions , cancellationToken : context . RequestAborted ) ;
299
+ }
300
+ else
301
+ {
302
+ await context . Response . WriteAsync ( "Accepted" ) ;
303
+ }
280
304
} ) ;
281
305
}
282
306
0 commit comments