@@ -19,11 +19,13 @@ public sealed class StdioServerTransport : TransportBase, IServerTransport
19
19
{
20
20
private readonly string _serverName ;
21
21
private readonly ILogger _logger ;
22
- private readonly JsonSerializerOptions _jsonOptions ;
22
+
23
+ private readonly JsonSerializerOptions _jsonOptions = JsonSerializerOptionsExtensions . DefaultOptions ;
24
+ private readonly TextReader _stdin = Console . In ;
25
+ private readonly TextWriter _stdout = Console . Out ;
26
+
23
27
private Task ? _readTask ;
24
28
private CancellationTokenSource ? _shutdownCts ;
25
- private readonly TextReader _input ;
26
- private readonly TextWriter _output ;
27
29
28
30
private string EndpointName => $ "Server (stdio) ({ _serverName } )";
29
31
@@ -59,62 +61,12 @@ public StdioServerTransport(McpServerOptions serverOptions, ILoggerFactory? logg
59
61
/// </para>
60
62
/// </remarks>
61
63
public StdioServerTransport ( string serverName , ILoggerFactory ? loggerFactory = null )
62
- : this ( serverName ?? throw new ArgumentNullException ( nameof ( serverName ) ) , Console . In , Console . Out , loggerFactory )
63
- {
64
- }
65
-
66
- /// <summary>
67
- /// Initializes a new instance of the <see cref="StdioServerTransport"/> class using the specified
68
- /// <paramref name="input"/> and <paramref name="output"/> streams.
69
- /// </summary>
70
- /// <param name="serverOptions">The server options.</param>
71
- /// <param name="input">The reader, from which all input is read.</param>
72
- /// <param name="output">The writer, to which all output is written.</param>
73
- /// <param name="loggerFactory">Optional logger factory used for logging employed by the transport.</param>
74
- /// <exception cref="ArgumentNullException"><paramref name="serverOptions"/> is <see langword="null"/> or contains a null name.</exception>
75
- /// <exception cref="ArgumentNullException"><paramref name="input"/> is <see langword="null"/>.</exception>
76
- /// <exception cref="ArgumentNullException"><paramref name="output"/> is <see langword="null"/>.</exception>
77
- /// <remarks>
78
- /// <para>
79
- /// By default, no logging is performed. If a <paramref name="loggerFactory"/> is supplied, it must not log
80
- /// to <see cref="Console.Out"/>, as that will interfere with the transport's output.
81
- /// </para>
82
- /// </remarks>
83
- public StdioServerTransport ( McpServerOptions serverOptions , TextReader input , TextWriter output , ILoggerFactory ? loggerFactory = null )
84
- : this ( GetServerName ( serverOptions ) , input , output , loggerFactory )
85
- {
86
- }
87
-
88
- /// <summary>
89
- /// Initializes a new instance of the <see cref="StdioServerTransport"/> class using the specified
90
- /// <paramref name="input"/> and <paramref name="output"/> streams.
91
- /// </summary>
92
- /// <param name="serverName">The name of the server.</param>
93
- /// <param name="input">The reader, from which all input is read.</param>
94
- /// <param name="output">The writer, to which all output is written.</param>
95
- /// <param name="loggerFactory">Optional logger factory used for logging employed by the transport.</param>
96
- /// <exception cref="ArgumentNullException"><paramref name="serverName"/> is <see langword="null"/>.</exception>
97
- /// <exception cref="ArgumentNullException"><paramref name="input"/> is <see langword="null"/>.</exception>
98
- /// <exception cref="ArgumentNullException"><paramref name="output"/> is <see langword="null"/>.</exception>
99
- /// <remarks>
100
- /// <para>
101
- /// By default, no logging is performed. If a <paramref name="loggerFactory"/> is supplied, it must not log
102
- /// to <see cref="Console.Out"/>, as that will interfere with the transport's output.
103
- /// </para>
104
- /// </remarks>
105
- public StdioServerTransport ( string serverName , TextReader input , TextWriter output , ILoggerFactory ? loggerFactory = null )
106
64
: base ( loggerFactory )
107
65
{
108
66
Throw . IfNull ( serverName ) ;
109
- Throw . IfNull ( input ) ;
110
- Throw . IfNull ( output ) ;
111
-
67
+
112
68
_serverName = serverName ;
113
- _input = input ;
114
- _output = output ;
115
-
116
69
_logger = ( ILogger ? ) loggerFactory ? . CreateLogger < StdioClientTransport > ( ) ?? NullLogger . Instance ;
117
- _jsonOptions = JsonSerializerOptionsExtensions . DefaultOptions ;
118
70
}
119
71
120
72
/// <inheritdoc/>
@@ -149,8 +101,8 @@ public override async Task SendMessageAsync(IJsonRpcMessage message, Cancellatio
149
101
var json = JsonSerializer . Serialize ( message , _jsonOptions ) ;
150
102
_logger . TransportSendingMessage ( EndpointName , id , json ) ;
151
103
152
- await _output . WriteLineAsync ( json . AsMemory ( ) , cancellationToken ) . ConfigureAwait ( false ) ;
153
- await _output . FlushAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
104
+ await _stdout . WriteLineAsync ( json . AsMemory ( ) , cancellationToken ) . ConfigureAwait ( false ) ;
105
+ await _stdout . FlushAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
154
106
155
107
_logger . TransportSentMessage ( EndpointName , id ) ;
156
108
}
@@ -178,7 +130,7 @@ private async Task ReadMessagesAsync(CancellationToken cancellationToken)
178
130
{
179
131
_logger . TransportWaitingForMessage ( EndpointName ) ;
180
132
181
- var reader = _input ;
133
+ var reader = _stdin ;
182
134
var line = await reader . ReadLineAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
183
135
if ( line == null )
184
136
{
0 commit comments