File tree Expand file tree Collapse file tree
src/ModelContextProtocol.Core/Client Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -187,14 +187,29 @@ public async Task<ITransport> ConnectAsync(CancellationToken cancellationToken =
187187 lock ( s_consoleEncodingLock )
188188 {
189189 Encoding originalInputEncoding = Console . InputEncoding ;
190+ bool encodingChanged = false ;
190191 try
191192 {
192- Console . InputEncoding = StreamClientSessionTransport . NoBomUtf8Encoding ;
193+ try
194+ {
195+ Console . InputEncoding = StreamClientSessionTransport . NoBomUtf8Encoding ;
196+ encodingChanged = true ;
197+ }
198+ catch
199+ {
200+ // Host has no usable console (e.g. WPF/WinForms on .NET Framework with no
201+ // AllocConsole). The child inherits the current Console.InputEncoding;
202+ // non-ASCII stdin may be misencoded, but the connect itself proceeds.
203+ }
204+
193205 processStarted = process . Start ( ) ;
194206 }
195207 finally
196208 {
197- Console . InputEncoding = originalInputEncoding ;
209+ if ( encodingChanged )
210+ {
211+ Console . InputEncoding = originalInputEncoding ;
212+ }
198213 }
199214 }
200215#endif
You can’t perform that action at this time.
0 commit comments