Path: redb-route/redb.Route.Llm.Mcp/Transport/HttpSseMcpClient.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm.Mcp/Transport/HttpSseMcpClient.cs
----
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using Microsoft.Extensions.Logging;
using redb.Route.Llm.Mcp.Protocol;
namespace redb.Route.Llm.Mcp.Transport;
///
/// HTTP + SSE transport. POSTs JSON-RPC requests to the base URL, opens a parallel
/// SSE channel (GET) for server-initiated frames (notifications, tools/list_changed).
/// Bearer auth via .
///
public sealed class HttpSseMcpClient : McpClientBase
{
private readonly McpTransport _transport;
private readonly HttpClient _http;
private readonly bool _ownsHttpClient;
private CancellationTokenSource? _sseCts;
private Task? _ssePump;
/// The client this transport sends through (tests read its transport defaults).
internal HttpClient Http => _http;
///
/// Transport of the client this class creates itself: HTTP/2 keep-alive pings every 15 seconds while a request
/// is in flight. The SSE stream stays open and can be silent between events for minutes, and VPN tunnels, NAT
/// and proxies drop a connection that is silent for about 50 seconds. A copy of
/// redb.Route.Llm.Providers.LlmHttpTransport.BuildHandler (this package does not reference
/// redb.Route.Llm): change both together. A client the host passes in is used as it is.
///
private static SocketsHttpHandler BuildKeepAliveHandler() => new()
{
PooledConnectionLifetime = TimeSpan.FromMinutes(5),
KeepAlivePingDelay = TimeSpan.FromSeconds(15),
KeepAlivePingTimeout = TimeSpan.FromSeconds(20),
KeepAlivePingPolicy = HttpKeepAlivePingPolicy.WithActiveRequests,
};
/// Creates a new HTTP+SSE MCP client.
/// Logical server name.
/// HTTP+SSE transport configuration.
/// Logger.
/// Optional HTTP client (DI-managed); a private one is created if null.
public HttpSseMcpClient(string serverName, McpTransport transport, ILogger logger, HttpClient? httpClient = null)
: base(serverName, logger)
{
ArgumentNullException.ThrowIfNull(transport);
if (transport.Kind != McpTransportKind.HttpSse)
throw new ArgumentException("HttpSseMcpClient requires an HTTP+SSE transport.", nameof(transport));
if (string.IsNullOrWhiteSpace(transport.BaseUrl))
throw new ArgumentException("HTTP+SSE transport requires a BaseUrl.", nameof(transport));
_transport = transport;
if (httpClient is not null)
{
_http = httpClient;
_ownsHttpClient = false;
}
else
{
_http = new HttpClient(BuildKeepAliveHandler())
{
BaseAddress = new Uri(transport.BaseUrl, UriKind.Absolute),
// HTTP/2 when the server offers it, so the keep-alive pings have a frame to ride on.
DefaultRequestVersion = HttpVersion.Version20,
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower,
};
_ownsHttpClient = true;
}
if (!string.IsNullOrEmpty(transport.BearerToken))
_http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", transport.BearerToken);
}
///
protected override Task StartTransportAsync(CancellationToken cancellationToken)
{
_sseCts = new CancellationTokenSource();
_ssePump = Task.Run(() => SsePumpAsync(_sseCts.Token));
return Task.CompletedTask;
}
///
protected override async Task StopTransportAsync()
{
try { _sseCts?.Cancel(); } catch { /* ignored */ }
try { if (_ssePump is not null) await _ssePump.ConfigureAwait(false); }
catch { /* pump exceptions surface via OnTransportFailed */ }
_ssePump = null;
_sseCts?.Dispose();
_sseCts = null;
if (_ownsHttpClient)
_http.Dispose();
}
///
protected override async Task SendFrameAsync(string frameJson, CancellationToken cancellationToken)
{
using var content = new StringContent(frameJson, Encoding.UTF8, "application/json");
using var response = await _http.PostAsync(_transport.BaseUrl, content, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
// Some MCP servers return the JSON-RPC response inline; others stream via SSE.
// Fold inline replies through the same demux path.
var body = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrWhiteSpace(body) && body.AsSpan().TrimStart().Length > 0 && body.AsSpan().TrimStart()[0] == '{')
OnFrameReceived(body);
}
private async Task SsePumpAsync(CancellationToken ct)
{
try
{
// A hand-built request starts at HTTP/1.1 whatever the client asks for; carry the client's version over,
// or the keep-alive pings never run on the stream.
using var req = new HttpRequestMessage(HttpMethod.Get, _transport.BaseUrl)
{
Version = _http.DefaultRequestVersion,
VersionPolicy = _http.DefaultVersionPolicy,
};
req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));
using var response = await _http.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
Logger.LogTrace("MCP {Server} SSE channel returned {Status}; skipping pump.", ServerName, response.StatusCode);
return;
}
using var stream = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false);
using var reader = new StreamReader(stream, Encoding.UTF8);
var dataBuffer = new StringBuilder();
while (!ct.IsCancellationRequested)
{
var line = await reader.ReadLineAsync(ct).ConfigureAwait(false);
if (line is null) break;
if (line.Length == 0)
{
if (dataBuffer.Length > 0)
{
var frame = dataBuffer.ToString();
dataBuffer.Clear();
OnFrameReceived(frame);
}
continue;
}
if (line.StartsWith("data:", StringComparison.Ordinal))
{
if (dataBuffer.Length > 0) dataBuffer.Append('\n');
dataBuffer.Append(line.AsSpan(5).TrimStart());
}
// Other SSE fields (event:, id:, retry:) are ignored — MCP only uses data:.
}
}
catch (OperationCanceledException) { /* normal shutdown */ }
catch (Exception ex)
{
Logger.LogWarning(ex, "MCP {Server} SSE pump terminated.", ServerName);
OnTransportFailed($"SSE pump error: {ex.Message}");
}
}
}