Skip to content

Modify the wpf to connect to the mcp server using the stdio method #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/ModelContextProtocol.Core/Client/StdioClientTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,21 @@ public async Task<ITransport> ConnectAsync(CancellationToken cancellationToken =
// up the encoding from Console.InputEncoding. As such, when not targeting .NET Core,
// we temporarily change Console.InputEncoding to no-BOM UTF-8 around the Process.Start
// call, to ensure it picks up the correct encoding.
#if NET
processStarted = process.Start();
#else

Encoding originalInputEncoding = Console.InputEncoding;
try
if (HasConsole())
{
Console.InputEncoding = StreamClientSessionTransport.NoBomUtf8Encoding;
processStarted = process.Start();
}
finally
{
Console.InputEncoding = originalInputEncoding;
try
{
Console.InputEncoding = StreamClientSessionTransport.NoBomUtf8Encoding;
}
finally
{
Console.InputEncoding = originalInputEncoding;
}
}
#endif

processStarted = process.Start();

if (!processStarted)
{
Expand Down Expand Up @@ -199,7 +200,13 @@ public async Task<ITransport> ConnectAsync(CancellationToken cancellationToken =
throw new IOException("Failed to connect transport.", ex);
}
}
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();

private static bool HasConsole()
{
try { return GetConsoleWindow() != IntPtr.Zero; } catch { return false; }
}
internal static void DisposeProcess(
Process? process, bool processRunning, TimeSpan shutdownTimeout, string endpointName)
{
Expand Down