Skip to content

Commit 63d6ed8

Browse files
committed
wait for server task to finish
1 parent 4035096 commit 63d6ed8

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/ModelContextProtocol/Protocol/Transport/InMemoryTransport.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Logging;
33
using Microsoft.Extensions.Logging.Abstractions;
44

@@ -203,9 +203,9 @@ private async Task ConnectInternalAsync(
203203
// Configure server options
204204
var serverOptions = new McpServerOptions
205205
{
206-
ServerInfo = new Protocol.Types.Implementation { Name = "InMemoryServer", Version = "1.0" },
206+
ServerInfo = new Types.Implementation { Name = "InMemoryServer", Version = "1.0" },
207207
ProtocolVersion = "2024",
208-
Capabilities = new Protocol.Types.ServerCapabilities()
208+
Capabilities = new Types.ServerCapabilities()
209209
};
210210

211211
services.AddOptions<McpServerOptions>().Configure(options =>
@@ -320,6 +320,20 @@ private async Task CleanupAsync(CancellationToken cancellationToken = default)
320320
// Complete the shared channel
321321
_sharedChannel.Writer.Complete();
322322

323+
// now wait for the server task to complete
324+
if (_serverTask != null)
325+
{
326+
try
327+
{
328+
_logger.TransportWaitingForReadTask(_endpointName);
329+
await Task.WhenAny(_serverTask, Task.Delay(500, cancellationToken));
330+
}
331+
catch (Exception ex)
332+
{
333+
_logger.TransportCleanupReadTaskFailed(_endpointName, ex);
334+
}
335+
}
336+
323337
// Then cancel the server and tasks
324338
if (_cancellationTokenSource != null)
325339
{

0 commit comments

Comments
 (0)