-
Notifications
You must be signed in to change notification settings - Fork 656
Expand file tree
/
Copy pathStreamClientSessionTransport.cs
More file actions
190 lines (171 loc) · 6.89 KB
/
StreamClientSessionTransport.cs
File metadata and controls
190 lines (171 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Protocol;
using System.Buffers;
using System.IO.Pipelines;
using System.Text.Json;
namespace ModelContextProtocol.Client;
/// <summary>Provides the client side of a stream-based session transport.</summary>
internal class StreamClientSessionTransport : TransportBase
{
private static readonly byte[] s_newlineBytes = "\n"u8.ToArray();
private static readonly StreamPipeReaderOptions s_pipeReaderOptions = new(bufferSize: 64 * 1024); // 64KB minimum buffer
private readonly PipeReader _serverOutputPipe;
private readonly Stream _serverInputStream;
private readonly SemaphoreSlim _sendLock = new(1, 1);
private CancellationTokenSource? _shutdownCts = new();
private Task? _readTask;
/// <summary>
/// Initializes a new instance of the <see cref="StreamClientSessionTransport"/> class.
/// </summary>
/// <param name="serverInput">
/// The server's input stream. Messages written to this stream will be sent to the server.
/// </param>
/// <param name="serverOutput">
/// The server's output stream. Messages read from this stream will be received from the server.
/// </param>
/// <param name="endpointName">
/// A name that identifies this transport endpoint in logs.
/// </param>
/// <param name="loggerFactory">
/// Optional factory for creating loggers. If null, a NullLogger is used.
/// </param>
/// <remarks>
/// This constructor starts a background task to read messages from the server output stream.
/// The transport will be marked as connected once initialized.
/// </remarks>
public StreamClientSessionTransport(Stream serverInput, Stream serverOutput, string endpointName, ILoggerFactory? loggerFactory)
: base(endpointName, loggerFactory)
{
Throw.IfNull(serverInput);
Throw.IfNull(serverOutput);
_serverInputStream = serverInput;
_serverOutputPipe = PipeReader.Create(serverOutput, s_pipeReaderOptions);
SetConnected();
// Start reading messages in the background. We use the rarer pattern of new Task + Start
// in order to ensure that the body of the task will always see _readTask initialized.
// It is then able to reliably null it out on completion.
var readTask = new Task<Task>(
thisRef => ((StreamClientSessionTransport)thisRef!).ReadMessagesAsync(_shutdownCts.Token),
this,
TaskCreationOptions.DenyChildAttach);
_readTask = readTask.Unwrap();
readTask.Start();
}
/// <inheritdoc/>
public override async Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default)
{
string id = "(no id)";
if (message is JsonRpcMessageWithId messageWithId)
{
id = messageWithId.Id.ToString();
}
LogTransportSendingMessageSensitive(message);
using var _ = await _sendLock.LockAsync(cancellationToken).ConfigureAwait(false);
try
{
var json = JsonSerializer.SerializeToUtf8Bytes(message, McpJsonUtilities.JsonContext.Default.JsonRpcMessage);
await _serverInputStream.WriteAsync(json, cancellationToken).ConfigureAwait(false);
await _serverInputStream.WriteAsync(s_newlineBytes, cancellationToken).ConfigureAwait(false);
await _serverInputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
LogTransportSendFailed(Name, id, ex);
throw new IOException("Failed to send message.", ex);
}
}
/// <inheritdoc/>
public override ValueTask DisposeAsync() =>
CleanupAsync(cancellationToken: CancellationToken.None);
private async Task ReadMessagesAsync(CancellationToken cancellationToken)
{
Exception? error = null;
try
{
LogTransportEnteringReadMessagesLoop(Name);
await _serverOutputPipe.ReadLinesAsync(ProcessLineAsync, cancellationToken).ConfigureAwait(false);
LogTransportEndOfStream(Name);
}
catch (OperationCanceledException)
{
LogTransportReadMessagesCancelled(Name);
}
catch (Exception ex)
{
error = ex;
LogTransportReadMessagesFailed(Name, ex);
}
finally
{
_readTask = null;
await CleanupAsync(error, cancellationToken).ConfigureAwait(false);
}
}
private async Task ProcessLineAsync(ReadOnlySequence<byte> line, CancellationToken cancellationToken)
{
if (Logger.IsEnabled(LogLevel.Trace))
{
LogTransportReceivedMessageSensitive(Name, EncodingUtilities.GetUtf8String(line));
}
try
{
JsonRpcMessage? message;
if (line.IsSingleSegment)
{
message = JsonSerializer.Deserialize(line.First.Span, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonRpcMessage))) as JsonRpcMessage;
}
else
{
var reader = new Utf8JsonReader(line, isFinalBlock: true, state: default);
message = JsonSerializer.Deserialize(ref reader, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonRpcMessage))) as JsonRpcMessage;
}
if (message is not null)
{
await WriteMessageAsync(message, cancellationToken).ConfigureAwait(false);
}
else
{
if (Logger.IsEnabled(LogLevel.Trace))
{
LogTransportMessageParseUnexpectedTypeSensitive(Name, EncodingUtilities.GetUtf8String(line));
}
}
}
catch (JsonException ex)
{
if (Logger.IsEnabled(LogLevel.Trace))
{
LogTransportMessageParseFailedSensitive(Name, EncodingUtilities.GetUtf8String(line), ex);
}
else
{
LogTransportMessageParseFailed(Name, ex);
}
}
}
protected virtual async ValueTask CleanupAsync(Exception? error = null, CancellationToken cancellationToken = default)
{
LogTransportShuttingDown(Name);
if (Interlocked.Exchange(ref _shutdownCts, null) is { } shutdownCts)
{
await shutdownCts.CancelAsync().ConfigureAwait(false);
shutdownCts.Dispose();
}
if (Interlocked.Exchange(ref _readTask, null) is Task readTask)
{
try
{
await readTask.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
LogTransportCleanupReadTaskFailed(Name, ex);
}
}
SetDisconnected(error);
LogTransportShutDown(Name);
}
}