Skip to content

Commit becb0a7

Browse files
committed
Move WriteJsonRpcMessageToBuffer to method
1 parent 25bcb44 commit becb0a7

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/ModelContextProtocol.AspNetCore/McpEndpointRouteBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static IEndpointConventionBuilder MapMcp(this IEndpointRouteBuilder endpo
8585

8686
if (!_sessions.TryGetValue(sessionId.ToString(), out var transport))
8787
{
88-
await Results.BadRequest($"Session {sessionId} not found.").ExecuteAsync(context);
88+
await Results.BadRequest($"Session ID not found.").ExecuteAsync(context);
8989
return;
9090
}
9191

src/ModelContextProtocol/Protocol/Transport/SseResponseStreamTransport.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,30 @@ public sealed class SseResponseStreamTransport(Stream sseResponseStream, string
3232
/// <returns>A task representing the send loop that writes JSON-RPC messages to the SSE response stream.</returns>
3333
public Task RunAsync(CancellationToken cancellationToken)
3434
{
35-
void WriteJsonRpcMessageToBuffer(SseItem<IJsonRpcMessage?> item, IBufferWriter<byte> writer)
36-
{
37-
if (item.EventType == "endpoint")
38-
{
39-
writer.Write(Encoding.UTF8.GetBytes(messageEndpoint));
40-
return;
41-
}
42-
43-
JsonSerializer.Serialize(GetUtf8JsonWriter(writer), item.Data, McpJsonUtilities.DefaultOptions.GetTypeInfo<IJsonRpcMessage?>());
44-
}
45-
46-
IsConnected = true;
47-
4835
// The very first SSE event isn't really an IJsonRpcMessage, but there's no API to write a single item of a different type,
4936
// so we fib and special-case the "endpoint" event type in the formatter.
5037
if (!_outgoingSseChannel.Writer.TryWrite(new SseItem<IJsonRpcMessage?>(null, "endpoint")))
5138
{
5239
throw new InvalidOperationException($"You must call ${nameof(RunAsync)} before calling ${nameof(SendMessageAsync)}.");
5340
}
5441

42+
IsConnected = true;
43+
5544
var sseItems = _outgoingSseChannel.Reader.ReadAllAsync(cancellationToken);
5645
return _sseWriteTask = SseFormatter.WriteAsync(sseItems, sseResponseStream, WriteJsonRpcMessageToBuffer, cancellationToken);
5746
}
5847

48+
private void WriteJsonRpcMessageToBuffer(SseItem<IJsonRpcMessage?> item, IBufferWriter<byte> writer)
49+
{
50+
if (item.EventType == "endpoint")
51+
{
52+
writer.Write(Encoding.UTF8.GetBytes(messageEndpoint));
53+
return;
54+
}
55+
56+
JsonSerializer.Serialize(GetUtf8JsonWriter(writer), item.Data, McpJsonUtilities.DefaultOptions.GetTypeInfo<IJsonRpcMessage?>());
57+
}
58+
5959
/// <inheritdoc/>
6060
public ChannelReader<IJsonRpcMessage> MessageReader => _incomingChannel.Reader;
6161

0 commit comments

Comments
 (0)