Use System.Net.ServerSentEvents for parsing SSE response (#32)

This commit is contained in:
Stephen Toub 2025-03-07 12:41:01 -05:00 • committed by GitHub
commit f15b94c244
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 20 deletions

View file

@ -1,4 +1,5 @@
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.ServerSentEvents;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using McpDotNet.Configuration; using McpDotNet.Configuration;
@ -191,30 +192,21 @@ public sealed class SseClientTransport : TransportBase, IClientTransport
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
using var reader = new StreamReader(stream);
// Reset reconnect attempts on successful connection // Reset reconnect attempts on successful connection
reconnectAttempts = 0; reconnectAttempts = 0;
string? currentEvent = null; await foreach (SseItem<string> sseEvent in SseParser.Create(stream).EnumerateAsync(cancellationToken))
string? line;
while ((line = await reader.ReadLineAsync(cancellationToken)) != null)
{ {
if (line.StartsWith("event: ")) switch (sseEvent.EventType)
{ {
currentEvent = line.Substring(7); case "endpoint":
} HandleEndpointEvent(sseEvent.Data);
else if (line.StartsWith("data: ")) break;
{
var data = line.Substring(6); case "message":
if (currentEvent == "endpoint") await ProcessSseMessage(sseEvent.Data, cancellationToken);
{ break;
HandleEndpointEvent(data);
}
else
{
await ProcessSseMessage(data, cancellationToken);
}
} }
} }
} }

View file

@ -11,7 +11,7 @@
<PackageId>mcpdotnet</PackageId> <PackageId>mcpdotnet</PackageId>
<Version>1.0.1.3</Version> <Version>1.0.1.3</Version>
<Authors>PederHP</Authors> <Authors>PederHP</Authors>
<Description>.NET client library for the Model Context Protocol (MCP)</Description> <Description>.NET library for the Model Context Protocol (MCP)</Description>
<PackageProjectUrl>https://github.com/PederHP/mcpdotnet</PackageProjectUrl> <PackageProjectUrl>https://github.com/PederHP/mcpdotnet</PackageProjectUrl>
<RepositoryUrl>https://github.com/PederHP/mcpdotnet</RepositoryUrl> <RepositoryUrl>https://github.com/PederHP/mcpdotnet</RepositoryUrl>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
@ -37,6 +37,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="System.Net.ServerSentEvents" Version="9.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>