Skip to content

Commit de552b3

Browse files
committed
Example set of changes to support an optional Endpoint
1 parent b9820d6 commit de552b3

5 files changed

Lines changed: 13 additions & 12 deletions

File tree

samples/aspire/McpSample.Client/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
var endpoint = httpClient.BaseAddress?.ToString().Replace("https+", "") ?? throw new InvalidOperationException("No endpoint set for MCP server");
1616

17-
var transport = new SseClientTransport(new SseClientTransportOptions {
18-
Endpoint = new(endpoint)
19-
}, httpClient);
17+
var transport = new SseClientTransport(new SseClientTransportOptions(), httpClient);
2018

2119
var t = McpClientFactory.CreateAsync(transport);
2220
t.Wait();

src/ModelContextProtocol.Core/Client/SseClientSessionTransport.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ internal sealed partial class SseClientSessionTransport : TransportBase
1717
{
1818
private readonly HttpClient _httpClient;
1919
private readonly SseClientTransportOptions _options;
20-
private readonly Uri _sseEndpoint;
2120
private Uri? _messageEndpoint;
2221
private readonly CancellationTokenSource _connectionCts;
2322
private Task? _receiveTask;
@@ -40,7 +39,6 @@ public SseClientSessionTransport(
4039
Throw.IfNull(httpClient);
4140

4241
_options = transportOptions;
43-
_sseEndpoint = transportOptions.Endpoint;
4442
_httpClient = httpClient;
4543
_connectionCts = new CancellationTokenSource();
4644
_logger = (ILogger?)loggerFactory?.CreateLogger<SseClientTransport>() ?? NullLogger.Instance;
@@ -150,7 +148,7 @@ private async Task ReceiveMessagesAsync(CancellationToken cancellationToken)
150148
{
151149
try
152150
{
153-
using var request = new HttpRequestMessage(HttpMethod.Get, _sseEndpoint);
151+
using var request = new HttpRequestMessage(HttpMethod.Get, _httpClient.BaseAddress);
154152
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));
155153
StreamableHttpClientSessionTransport.CopyAdditionalHeaders(request.Headers, _options.AdditionalHeaders, sessionId: null, protocolVersion: null);
156154

@@ -239,8 +237,8 @@ private void HandleEndpointEvent(string data)
239237
return;
240238
}
241239

242-
// If data is an absolute URL, the Uri will be constructed entirely from it and not the _sseEndpoint.
243-
_messageEndpoint = new Uri(_sseEndpoint, data);
240+
// If data is an absolute URL, the Uri will be constructed entirely from it and not the _httpClient.BaseAddress.
241+
_messageEndpoint = new Uri(_httpClient.BaseAddress!, data);
244242

245243
// Set connected state
246244
SetConnected();

src/ModelContextProtocol.Core/Client/SseClientTransport.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed class SseClientTransport : IClientTransport, IAsyncDisposable
2525
/// <param name="transportOptions">Configuration options for the transport.</param>
2626
/// <param name="loggerFactory">Logger factory for creating loggers used for diagnostic output during transport operations.</param>
2727
public SseClientTransport(SseClientTransportOptions transportOptions, ILoggerFactory? loggerFactory = null)
28-
: this(transportOptions, new HttpClient(), loggerFactory, ownsHttpClient: true)
28+
: this(transportOptions, new HttpClient { BaseAddress = transportOptions.Endpoint ?? throw new InvalidOperationException("No Endpoint provided on the transport options.") }, loggerFactory, ownsHttpClient: true)
2929
{
3030
}
3131

@@ -44,11 +44,16 @@ public SseClientTransport(SseClientTransportOptions transportOptions, HttpClient
4444
Throw.IfNull(transportOptions);
4545
Throw.IfNull(httpClient);
4646

47+
if (httpClient.BaseAddress is null)
48+
{
49+
throw new InvalidOperationException("No BaseAddress set on the HttpClient. Please set the BaseAddress to the server's endpoint.");
50+
}
51+
4752
_options = transportOptions;
4853
_httpClient = httpClient;
4954
_loggerFactory = loggerFactory;
5055
_ownsHttpClient = ownsHttpClient;
51-
Name = transportOptions.Name ?? transportOptions.Endpoint.ToString();
56+
Name = transportOptions.Name ?? httpClient.BaseAddress.ToString();
5257
}
5358

5459
/// <inheritdoc />

src/ModelContextProtocol.Core/Client/SseClientTransportOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public sealed class SseClientTransportOptions
88
/// <summary>
99
/// Gets or sets the base address of the server for SSE connections.
1010
/// </summary>
11-
public required Uri Endpoint
11+
public Uri? Endpoint
1212
{
1313
get;
1414
set

tests/ModelContextProtocol.AspNetCore.Tests/HttpServerIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public async Task Connect_TestServer_ShouldProvideServerFields()
5353
Assert.NotNull(client.ServerCapabilities);
5454
Assert.NotNull(client.ServerInfo);
5555

56-
if (ClientTransportOptions.Endpoint.AbsolutePath.EndsWith("/sse"))
56+
if (ClientTransportOptions.Endpoint is not null && ClientTransportOptions.Endpoint.AbsolutePath.EndsWith("/sse"))
5757
{
5858
Assert.Null(client.SessionId);
5959
}

0 commit comments

Comments
 (0)