Enable netfx testing. (#588)
* Enable netfx testing. * Skip impacted tests and re-enable netfx testing. * Remove redundant comment. * Remove unnecessary project configuration. * Simplify test server copying.
This commit is contained in:
parent
c0440760ac
commit
aa75f6f37e
7 changed files with 1940 additions and 53 deletions
|
|
@ -13,7 +13,7 @@ internal sealed class StdioClientSessionTransport : StreamClientSessionTransport
|
|||
private int _cleanedUp = 0;
|
||||
|
||||
public StdioClientSessionTransport(StdioClientTransportOptions options, Process process, string endpointName, Queue<string> stderrRollingLog, ILoggerFactory? loggerFactory)
|
||||
: base(process.StandardInput, process.StandardOutput, endpointName, loggerFactory)
|
||||
: base(process.StandardInput.BaseStream, process.StandardOutput.BaseStream, encoding: null, endpointName, loggerFactory)
|
||||
{
|
||||
_process = process;
|
||||
_options = options;
|
||||
|
|
|
|||
|
|
@ -69,8 +69,6 @@ public sealed partial class StdioClientTransport : IClientTransport
|
|||
{
|
||||
LogTransportConnecting(logger, endpointName);
|
||||
|
||||
UTF8Encoding noBomUTF8 = new(encoderShouldEmitUTF8Identifier: false);
|
||||
|
||||
ProcessStartInfo startInfo = new()
|
||||
{
|
||||
FileName = command,
|
||||
|
|
@ -80,10 +78,10 @@ public sealed partial class StdioClientTransport : IClientTransport
|
|||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
WorkingDirectory = _options.WorkingDirectory ?? Environment.CurrentDirectory,
|
||||
StandardOutputEncoding = noBomUTF8,
|
||||
StandardErrorEncoding = noBomUTF8,
|
||||
StandardOutputEncoding = StreamClientSessionTransport.NoBomUtf8Encoding,
|
||||
StandardErrorEncoding = StreamClientSessionTransport.NoBomUtf8Encoding,
|
||||
#if NET
|
||||
StandardInputEncoding = noBomUTF8,
|
||||
StandardInputEncoding = StreamClientSessionTransport.NoBomUtf8Encoding,
|
||||
#endif
|
||||
};
|
||||
|
||||
|
|
@ -164,7 +162,7 @@ public sealed partial class StdioClientTransport : IClientTransport
|
|||
Encoding originalInputEncoding = Console.InputEncoding;
|
||||
try
|
||||
{
|
||||
Console.InputEncoding = noBomUTF8;
|
||||
Console.InputEncoding = StreamClientSessionTransport.NoBomUtf8Encoding;
|
||||
processStarted = process.Start();
|
||||
}
|
||||
finally
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using ModelContextProtocol.Protocol;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ModelContextProtocol.Client;
|
||||
|
|
@ -7,6 +8,8 @@ namespace ModelContextProtocol.Client;
|
|||
/// <summary>Provides the client side of a stream-based session transport.</summary>
|
||||
internal class StreamClientSessionTransport : TransportBase
|
||||
{
|
||||
internal static UTF8Encoding NoBomUtf8Encoding { get; } = new(encoderShouldEmitUTF8Identifier: false);
|
||||
|
||||
private readonly TextReader _serverOutput;
|
||||
private readonly TextWriter _serverInput;
|
||||
private readonly SemaphoreSlim _sendLock = new(1, 1);
|
||||
|
|
@ -54,6 +57,43 @@ internal class StreamClientSessionTransport : TransportBase
|
|||
readTask.Start();
|
||||
}
|
||||
|
||||
/// <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="encoding">
|
||||
/// The encoding used for reading and writing messages from the input and output streams. Defaults to UTF-8 without BOM if null.
|
||||
/// </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 will be 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, Encoding? encoding, string endpointName, ILoggerFactory? loggerFactory)
|
||||
: this(
|
||||
new StreamWriter(serverInput, encoding ?? NoBomUtf8Encoding),
|
||||
#if NET
|
||||
new StreamReader(serverOutput, encoding ?? NoBomUtf8Encoding),
|
||||
#else
|
||||
new CancellableStreamReader(serverOutput, encoding ?? NoBomUtf8Encoding),
|
||||
#endif
|
||||
endpointName,
|
||||
loggerFactory)
|
||||
{
|
||||
Throw.IfNull(serverInput);
|
||||
Throw.IfNull(serverOutput);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override async Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -47,8 +47,9 @@ public sealed class StreamClientTransport : IClientTransport
|
|||
public Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Task.FromResult<ITransport>(new StreamClientSessionTransport(
|
||||
new StreamWriter(_serverInput),
|
||||
new StreamReader(_serverOutput),
|
||||
_serverInput,
|
||||
_serverOutput,
|
||||
encoding: null,
|
||||
"Client (stream)",
|
||||
_loggerFactory));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
<PackageId>ModelContextProtocol.Core</PackageId>
|
||||
<Description>Core .NET SDK for the Model Context Protocol (MCP)</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
|
||||
|
|
@ -20,6 +21,7 @@
|
|||
<!-- Dependencies only needed by netstandard2.0 -->
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||
<Compile Include="..\Common\Polyfills\**\*.cs" />
|
||||
<Compile Include="..\Common\CancellableStreamReader\**\*.cs" />
|
||||
<PackageReference Include="Microsoft.Bcl.Memory" />
|
||||
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
|
||||
<PackageReference Include="System.Text.Json" />
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public class McpServerPrimitiveCollection<T> : ICollection<T>, IReadOnlyCollecti
|
|||
/// </summary>
|
||||
public McpServerPrimitiveCollection(IEqualityComparer<string>? keyComparer = null)
|
||||
{
|
||||
_primitives = new(keyComparer);
|
||||
_primitives = new(keyComparer ?? EqualityComparer<string>.Default);
|
||||
}
|
||||
|
||||
/// <summary>Occurs when the collection is changed.</summary>
|
||||
|
|
|
|||
|
|
@ -46,7 +46,11 @@ public class StreamServerTransport : TransportBase
|
|||
|
||||
_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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue