Use System.Net.ServerSentEvents for parsing SSE response (#32)
This commit is contained in:
parent
c9bc402fb3
commit
f15b94c244
2 changed files with 13 additions and 20 deletions
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
@ -32,11 +32,12 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<InternalsVisibleTo Include="McpDotNet.Tests" />
|
<InternalsVisibleTo Include="McpDotNet.Tests" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<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>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue