Skip to content

Commit c1c551e

Browse files
committed
Guard against multiple RunAsync calls
1 parent 19fc7b7 commit c1c551e

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/ModelContextProtocol/Server/McpServer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal sealed class McpServer : McpJsonRpcEndpoint, IMcpServer
1515
private readonly EventHandler? _promptsChangedDelegate;
1616

1717
private string _endpointName;
18+
private int _started;
1819

1920
/// <summary>
2021
/// Creates a new instance of <see cref="McpServer"/>.
@@ -96,6 +97,11 @@ public McpServer(ITransport transport, McpServerOptions options, ILoggerFactory?
9697
/// <inheritdoc />
9798
public async Task RunAsync(CancellationToken cancellationToken = default)
9899
{
100+
if (Interlocked.Exchange(ref _started, 1) != 0)
101+
{
102+
throw new InvalidOperationException($"{nameof(RunAsync)} must only be called once.");
103+
}
104+
99105
try
100106
{
101107
using var _ = cancellationToken.Register(static s => ((McpServer)s!).CancelSession(), this);

0 commit comments

Comments
 (0)