-
Notifications
You must be signed in to change notification settings - Fork 653
Expand file tree
/
Copy pathStreamServerTransport.cs
More file actions
197 lines (171 loc) · 6.77 KB
/
StreamServerTransport.cs
File metadata and controls
197 lines (171 loc) · 6.77 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
191
192
193
194
195
196
197
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ModelContextProtocol.Protocol;
using System.Text;
using System.Text.Json;
namespace ModelContextProtocol.Server;
/// <summary>
/// Provides an <see cref="ITransport"/> implemented using a pair of input and output streams.
/// </summary>
/// <remarks>
/// The <see cref="StreamServerTransport"/> class implements bidirectional JSON-RPC messaging over arbitrary
/// streams, allowing MCP communication with clients through various I/O channels such as network sockets,
/// memory streams, or pipes.
/// </remarks>
public class StreamServerTransport : TransportBase
{
private static readonly byte[] s_newlineBytes = "\n"u8.ToArray();
private readonly ILogger _logger;
private readonly TextReader _inputReader;
private readonly Stream _outputStream;
private readonly SemaphoreSlim _sendLock = new(1, 1);
private readonly CancellationTokenSource _shutdownCts = new();
private readonly Task _readLoopCompleted;
private int _disposed = 0;
/// <summary>
/// Initializes a new instance of the <see cref="StreamServerTransport"/> class with explicit input/output streams.
/// </summary>
/// <param name="inputStream">The input <see cref="Stream"/> to use as standard input.</param>
/// <param name="outputStream">The output <see cref="Stream"/> to use as standard output.</param>
/// <param name="serverName">Optional name of the server, used for diagnostic purposes, like logging.</param>
/// <param name="loggerFactory">Optional logger factory used for logging employed by the transport.</param>
/// <exception cref="ArgumentNullException"><paramref name="inputStream"/> or <paramref name="outputStream"/> is <see langword="null"/>.</exception>
public StreamServerTransport(Stream inputStream, Stream outputStream, string? serverName = null, ILoggerFactory? loggerFactory = null)
: base(serverName is not null ? $"Server (stream) ({serverName})" : "Server (stream)", loggerFactory)
{
Throw.IfNull(inputStream);
Throw.IfNull(outputStream);
_logger = loggerFactory?.CreateLogger(GetType()) ?? NullLogger.Instance;
#if NET
_inputReader = new StreamReader(inputStream, Encoding.UTF8);
#else
_inputReader = new CancellableStreamReader(inputStream, Encoding.UTF8);
#endif
_outputStream = outputStream;
SetConnected();
_readLoopCompleted = Task.Run(ReadMessagesAsync, _shutdownCts.Token);
}
/// <inheritdoc/>
public override async Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default)
{
if (!IsConnected)
{
return;
}
using var _ = await _sendLock.LockAsync(cancellationToken).ConfigureAwait(false);
string id = "(no id)";
if (message is JsonRpcMessageWithId messageWithId)
{
id = messageWithId.Id.ToString();
}
try
{
var json = JsonSerializer.Serialize(message, McpJsonUtilities.JsonContext.Default.JsonRpcMessage);
LogTransportSendingMessageSensitive(Name, json);
await _outputStream.WriteAsync(Encoding.UTF8.GetBytes(json), cancellationToken).ConfigureAwait(false);
await _outputStream.WriteAsync(s_newlineBytes, cancellationToken).ConfigureAwait(false);
await _outputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
LogTransportSendFailed(Name, id, ex);
throw new IOException("Failed to send message.", ex);
}
}
private async Task ReadMessagesAsync()
{
CancellationToken shutdownToken = _shutdownCts.Token;
Exception? error = null;
try
{
LogTransportEnteringReadMessagesLoop(Name);
while (!shutdownToken.IsCancellationRequested)
{
var line = await _inputReader.ReadLineAsync(shutdownToken).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(line))
{
if (line is null)
{
LogTransportEndOfStream(Name);
break;
}
continue;
}
LogTransportReceivedMessageSensitive(Name, line);
try
{
if (JsonSerializer.Deserialize(line, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonRpcMessage))) is JsonRpcMessage message)
{
await WriteMessageAsync(message, shutdownToken).ConfigureAwait(false);
}
else
{
LogTransportMessageParseUnexpectedTypeSensitive(Name, line);
}
}
catch (JsonException ex)
{
if (Logger.IsEnabled(LogLevel.Trace))
{
LogTransportMessageParseFailedSensitive(Name, line, ex);
}
else
{
LogTransportMessageParseFailed(Name, ex);
}
// Continue reading even if we fail to parse a message
}
}
}
catch (OperationCanceledException)
{
LogTransportReadMessagesCancelled(Name);
}
catch (Exception ex)
{
LogTransportReadMessagesFailed(Name, ex);
error = ex;
}
finally
{
SetDisconnected(error);
}
}
/// <inheritdoc />
public override async ValueTask DisposeAsync()
{
if (Interlocked.Exchange(ref _disposed, 1) != 0)
{
return;
}
try
{
LogTransportShuttingDown(Name);
// Signal to the stdin reading loop to stop.
await _shutdownCts.CancelAsync().ConfigureAwait(false);
_shutdownCts.Dispose();
// Dispose of stdin/out. Cancellation may not be able to wake up operations
// synchronously blocked in a syscall; we need to forcefully close the handle / file descriptor.
_inputReader?.Dispose();
_outputStream?.Dispose();
// Make sure the work has quiesced.
try
{
await _readLoopCompleted.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
LogTransportCleanupReadTaskFailed(Name, ex);
}
}
finally
{
SetDisconnected();
LogTransportShutDown(Name);
}
GC.SuppressFinalize(this);
}
}